Reputation: 2337
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(
"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);
}
}
This is my xml file and the main class i want to show those values in the combobox.anyone please help
Upvotes: 13
Views: 64140
Reputation: 10240
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();
}
Upvotes: 0
Reputation: 161
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);
Upvotes: 1
Reputation: 3855
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(
"jacob.smith@example.com",
"isabella.johnson@example.com",
"ethan.williams@example.com",
"emma.jones@example.com",
"michael.brown@example.com");
}
}
Upvotes: 30