You can
add items to the combo box at any point of time.
Ex
hobbiesComboBox.getItems().add("CoCo");
hobbiesComboBox.getItems().addAll("Rugby",
"kabaddy");
Find the
below working application.
package com.sample.demos; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class ComboBoxApp extends Application { private static boolean isAdded = false; @Override public void start(Stage primaryStage) throws Exception { ObservableList<String> hobbies = FXCollections.observableArrayList("Tennis", "Football", "Cricket"); ComboBox<String> hobbiesComboBox = new ComboBox<>(hobbies); Button button = new Button("Add Some more hobbies"); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { if (!isAdded) { hobbiesComboBox.getItems().add("CoCo"); hobbiesComboBox.getItems().addAll("Rugby", "kabaddy"); isAdded = true; } } }); HBox hBox = new HBox(10, hobbiesComboBox, button); primaryStage.setScene(new Scene(hBox)); primaryStage.setTitle("Combo Box Example"); primaryStage.setWidth(900); primaryStage.setHeight(500); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(ComboBoxApp.class, args); } }
Run the above application and click on the combo box, you can able to see below kind of window.
After
click on the button ‘Add Some more hobbies’, you can able to see 3 more hobbies
added to the combo box.
No comments:
Post a Comment