【问题标题】:How to populate a list values to a combobox in JavaFx如何将列表值填充到 JavaFx 中的组合框
【发布时间】:2013-09-28 09:35:42
【问题描述】:

我有一个值列表,我想在 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 文件,也是我想在组合框中显示这些值的主类。请大家帮忙

【问题讨论】:

    标签: javafx


    【解决方案1】:

    您必须创建一个控制器并将其分配给您的 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");
    
    }
    }
    

    【讨论】:

    • @FXML public ComboBox myCombobox;
    【解决方案2】:

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

    ObservableList<String> options = 
            FXCollections.observableArrayList(
                "Option 1",
                "Option 2",
                "Option 3"
            );
        final ComboBox comboBox = new ComboBox(options);
    

    【讨论】:

      【解决方案3】:

      您可以使用 fxml 设置组合框中的项目

      <ComboBox fx:id="itemsCombobox">
          <items>
              <FXCollections fx:factory="observableArrayList">
                  <String fx:value="Item 1" />
                  <String fx:value="Item 2" />
                  <String fx:value="Item 3" />
              </FXCollections>
          </items>
      </ComboBox>
      

      在控制器中,您可以将第一项设置为默认值

      @FXML
      ComboBox itemsCombobox;
      public void initialize() {
        itemsCombobox.getSelectionModel().selectFirst();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-19
        • 2019-10-20
        • 1970-01-01
        • 2021-06-18
        • 2015-06-05
        • 1970-01-01
        • 2019-10-20
        相关资源
        最近更新 更多