11

我有一个值列表,我想在 javaFx 的组合框中填充这些值。这是我的 combo.xml

 <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-    Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<ComboBox id="comboId" layoutX="210.0" layoutY="108.0" prefHeight="27.0" prefWidth="102.0" promptText="Select">
  <items>
       <FXCollections fx:factory="observableArrayList">
      <String fx:value="Item 1" />
      <String fx:value="Item 2" />
      <String fx:value="Item 3" />
       </FXCollections>
     </items>
   </Com boBox>
  </children>
  </AnchorPane>

这是我的主要

public class JavaFXExperiment extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("combo.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    final ComboBox comboId = new ComboBox();
    comboId.getItems().addAll(
            "jacob.smith@example.com",
            "isabella.johnson@example.com",
            "ethan.williams@example.com",
            "emma.jones@example.com",
            "michael.brown@example.com");
}
  public static void main(String[] args) {
    launch(args);
}
}

这是我的 xml 文件和我想在组合框中显示这些值的主类。请帮助

4

2 回答 2

29

您必须创建一个控制器并将其分配给您的 FXML 屏幕。

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-    Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="MyController" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<ComboBox fx:id="myCombobox" id="comboId" layoutX="210.0" layoutY="108.0" prefHeight="27.0" prefWidth="102.0" promptText="Select">
  <items>
       <FXCollections fx:factory="observableArrayList">
      <String fx:value="Item 1" />
      <String fx:value="Item 2" />
      <String fx:value="Item 3" />
       </FXCollections>
     </items>
   </ComboBox>
  </children>
  </AnchorPane>

然后你的主要课程将是,

public class JavaFXExperiment extends Application {
@Override
public void start(Stage stage) throws Exception {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("combo.fxml"));
    Parent root = loader.load();

    MyController myController = loader.getController();

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

    //Set Data to FXML through controller
    myController.setData();
}
  public static void main(String[] args) {
    launch(args);
}
}

你的控制器将是,

public class  MyController implements Initializable
{

@FXML
public Combobox myCombobox;

@Override
    public void initialize(URL url, ResourceBundle rb) {
}

public void setData(){

myCombobox.getItems().clear();

myCombobox.getItems().addAll(
            "jacob.smith@example.com",
            "isabella.johnson@example.com",
            "ethan.williams@example.com",
            "emma.jones@example.com",
            "michael.brown@example.com");

}
}
于 2013-09-30T09:53:14.037 回答
0

创建组合框时,必须实例化 ComboBox 类并将项目定义为可观察列表,就像其他 UI 控件(如 ChoiceBox、ListView 和 TableView)在构造函数中设置项目一样。

ObservableList<String> options = 
        FXCollections.observableArrayList(
            "Option 1",
            "Option 2",
            "Option 3"
        );
    final ComboBox comboBox = new ComboBox(options);
于 2018-07-07T16:35:38.090 回答