'How to populate a list values to a combobox in JavaFx
I have a list of values which i want to populate in a combobox in javaFx. this is my 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>
this is my main
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(
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]");
}
public static void main(String[] args) {
launch(args);
}
}
This is my xml file and the main class i want to show those values in the combobox.anyone please help
Solution 1:[1]
You have to create one controller and assign it with your FXML Screen.
<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>
Then your main class will be,
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);
}
}
And Your controller will be,
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(
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]");
}
}
Solution 2:[2]
When creating a combo box, you must instantiate the ComboBox class and define the items as an observable list, just like other UI controls such as ChoiceBox, ListView, and TableView sets the items within a constructor.
ObservableList<String> options =
FXCollections.observableArrayList(
"Option 1",
"Option 2",
"Option 3"
);
final ComboBox comboBox = new ComboBox(options);
Solution 3:[3]
You can use the fxml to set items in the combobox
<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>
In the controller, you can set first item as default value
@FXML
ComboBox itemsCombobox;
public void initialize() {
itemsCombobox.getSelectionModel().selectFirst();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andrew Tobilko |
Solution 2 | Samer |
Solution 3 | Sorter |