2017-04-22 1 views
0

Je rencontre des problèmes pour afficher les options de zone de liste déroulante dans javafx et le générateur de scènes.JavaFX et Scene Builder n'affichent pas les options de zone de liste modifiable

D'abord, j'ai essayé d'utiliser un tableau, ce qui, je suppose, ne fonctionne pas. J'ai ensuite essayé d'utiliser un ObservableList<String> et créer une instance de la zone de liste déroulante comme

ComboBox combo = new ComboBox(list); 

Ce ne serait toujours pas afficher les options de combobox. classe contrôleur:

package javafxapplication1; 

import javafxapplication1.JavaFXApplication1; 
import java.net.URL; 
import static java.util.Collections.list; 
import java.util.ResourceBundle; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javax.lang.model.element.Element; 

/** 
* 
* @author KJ4CC 
*/ 
public class FXMLDocumentController implements Initializable { 
    ObservableList<String> options = 
    FXCollections.observableArrayList(
     "Option 1", 
     "Option 2", 
     "Option 3" 
    ); 

    @FXML 
    private Label label; 
    @FXML 
    private TextField dateText; 
    @FXML 
    private TextField time; 
    @FXML 
    private ComboBox band; 

    public void setTimeDate(){ 
     JavaFXApplication1 javaFXApp = new JavaFXApplication1(); 
     dateText.setText(javaFXApp.getDate()); 

    } 

    public void setTime(){ 
     JavaFXApplication1 javaFXApp = new JavaFXApplication1(); 
     time.setText(javaFXApp.getTime()); 

    } 
    @Override 

    public void initialize(URL url, ResourceBundle rb) { 
    band = new ComboBox(options); 

    }  

} 

Classe principale java:

public class JavaFXApplication1 extends Application { 
    private FXMLDocumentController initScene; 
    private DateFormat dtf; 
    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 

     stage.show(); 


    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 
    public String getDate(){ 
     dtf = new SimpleDateFormat("dd/MM/yy"); 
     Date dateobj = new Date(); 
     return dtf.format(dateobj); 
    } 
    public String getTime(){ 
     dtf = new SimpleDateFormat("HH:mm;ss"); 
     Date dateobj = new Date(); 
     return dtf.format(dateobj); 
    } 

} 

FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.geometry.Insets?> 
<?import javafx.scene.control.ComboBox?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.Menu?> 
<?import javafx.scene.control.MenuBar?> 
<?import javafx.scene.control.MenuItem?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.BorderPane?> 
<?import javafx.scene.layout.HBox?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.text.Text?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="383.0" prefWidth="590.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController"> 
    <top> 
     <VBox prefHeight="235.0" prefWidth="543.0" BorderPane.alignment="CENTER"> 
     <children> 
      <MenuBar> 
       <menus> 
       <Menu mnemonicParsing="false" text="File"> 
        <items> 
        <MenuItem mnemonicParsing="false" text="Close" /> 
        </items> 
       </Menu> 
       <Menu mnemonicParsing="false" text="Edit"> 
        <items> 
        <MenuItem mnemonicParsing="false" text="Delete" /> 
        </items> 
       </Menu> 
       <Menu mnemonicParsing="false" text="Help"> 
        <items> 
        <MenuItem mnemonicParsing="false" text="About" /> 
        </items> 
       </Menu> 
       </menus> 
      </MenuBar> 
      <TableView prefHeight="210.0" prefWidth="634.0"> 
       <columns> 
       <TableColumn prefWidth="75.0" text="C1" /> 
       <TableColumn prefWidth="75.0" text="C2" /> 
       </columns> 
       <VBox.margin> 
        <Insets left="2.0" right="2.0" /> 
       </VBox.margin> 
      </TableView> 
     </children> 
     </VBox> 
    </top> 
    <bottom> 
     <VBox prefHeight="160.0" prefWidth="676.0" BorderPane.alignment="CENTER"> 
     <children> 
      <HBox prefHeight="22.0" prefWidth="625.0"> 
       <children> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Date:" wrappingWidth="83.462890625"> 
        <HBox.margin> 
         <Insets left="5.0" right="20.0" /> 
        </HBox.margin> 
        </Text> 
        <Label onMouseClicked="#setTimeDate" /> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Time:" wrappingWidth="73.40625" /> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Call:" wrappingWidth="122.94921875"> 
        <HBox.margin> 
         <Insets left="5.0" /> 
        </HBox.margin> 
        </Text> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Band:" wrappingWidth="58.443359375" /> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Freq:" wrappingWidth="106.974609375" /> 
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Location:" /> 
       </children> 
      </HBox> 
      <HBox prefHeight="35.0" prefWidth="625.0"> 
       <children> 
        <TextField id="dateText" fx:id="dateText" prefHeight="25.0" prefWidth="94.0"> 
        <HBox.margin> 
         <Insets left="5.0" /> 
        </HBox.margin> 
        </TextField> 
        <TextField fx:id="time" layoutX="10.0" layoutY="10.0" prefHeight="25.0" prefWidth="71.0"> 
        <padding> 
         <Insets left="5.0" /> 
        </padding> 
        <HBox.margin> 
         <Insets left="10.0" right="10.0" /> 
        </HBox.margin> 
        </TextField> 
        <TextField layoutX="104.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0"> 
        <HBox.margin> 
         <Insets right="10.0" /> 
        </HBox.margin> 
        </TextField> 
        <ComboBox fx:id="band" prefHeight="25.0" prefWidth="57.0"> 
        <HBox.margin> 
         <Insets right="10.0" /> 
        </HBox.margin> 
        </ComboBox> 
        <TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0"> 
        <HBox.margin> 
         <Insets right="10.0" /> 
        </HBox.margin> 
        </TextField> 
        <TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0" /> 
       </children> 
      </HBox> 
      <HBox prefHeight="30.0" prefWidth="580.0"> 
       <children> 
        <Text fill="#14bdd7" onMouseClicked="#setTimeDate" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Date"> 
        <HBox.margin> 
         <Insets right="20.0" /> 
        </HBox.margin> 
        </Text> 
        <Text fill="#14bdd7" layoutX="10.0" layoutY="23.0" onMouseClicked="#setTime" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Time" /> 
       </children> 
      </HBox> 
     </children> 
     <BorderPane.margin> 
      <Insets left="5.0" right="5.0" /> 
     </BorderPane.margin> 
     </VBox> 
    </bottom> 
</BorderPane> 

Toute aide serait appréciée! Je suis un peu nouveau à cette substance de constructeur de scène et en utilisant javafx. Je suis juste en train de le contourner. Merci pour l'aide!

Répondre

-1

Ok, donc j'ai compris le problème. Je créais une nouvelle instance d'une zone de liste déroulante créant ainsi un vide. Voici la méthode avec le code de travail révisé.

public void initialize(URL url, ResourceBundle rb) { 
     ObservableList<String> options = 
    FXCollections.observableArrayList(
     "Option 1", 
     "Option 2", 
     "Option 3" 
    ); 
     band.setItems(options); 

    }