By
default, ComboBox widget do not set a value. If you want to set any default
value to ComboBox, use 'setValue' method.
TestFX.java
Ex
ObservableList<String>
hobbies = FXCollections.observableArrayList("Tennis",
"Football", "Cricket", "CoCO", "Rugby",
"kabaddy");
ComboBox<String>
hobbiesComboBox = new ComboBox<>(hobbies);
hobbiesComboBox.setVisibleRowCount(3);
hobbiesComboBox.setValue("Cricket");
Find the
below working application.
ComboBoxApp.java
package com.sample.demos; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class ComboBoxApp extends Application { @Override public void start(Stage primaryStage) throws Exception { ObservableList<String> hobbies = FXCollections.observableArrayList("Tennis", "Football", "Cricket", "CoCO", "Rugby", "kabaddy"); ComboBox<String> hobbiesComboBox = new ComboBox<>(hobbies); hobbiesComboBox.setVisibleRowCount(3); hobbiesComboBox.setValue("Cricket"); HBox hBox = new HBox(10, hobbiesComboBox); 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); } }
Thanks
ReplyDelete