JavaFX | ComboBox with examples
ComboBox is a part of the JavaFX library. JavaFX ComboBox is an implementation of simple ComboBox which shows a list of items out of which user can select at most one item, it inherits the class ComboBoxBase.
Constructors of ComboBox:
- ComboBox(): creates a default empty combo box
- ComboBox(ObservableList i): creates a combo box with the given items
Commonly used Methods:
| method | explanantion |
|---|---|
| getEditor() | gets the value of the property editor |
| getItems() | returns the items of the combo box |
| getVisibleRowCount() | returns the value of the property visibleRowCount. |
| setItems(ObservableList v) | Sets the tems of the combo box |
| setVisibleRowCount(int v) | sets the value of the property VisibleRowCount |
Below programs illustrate the ComboBox class of JavaFX:
- Program to create a Combo Box and add items to it: This program creates a ComboBox named combo_box and add a list of string to it using ChoiceBox(FXCollections.observableArrayList(week_days)). We would add the combo box and a label(description) to the tilepane(getChildren().add() function). We will create a stage (container) and add the tilepane to the scene and add the scene to the stage. We would display the stage using show() function.
// Java Program to create a combo Box and add items to itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjavafx.stage.Stage;importjavafx.scene.text.Text.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;publicclasscombo_box_1extendsApplication {// Launch the applicationpublicvoidstart(Stage stage){// Set title for the stagestage.setTitle("creating combo box ");// Create a tile paneTilePane r =newTilePane();// Create a labelLabel description_label =newLabel("This is a combo box example ");// WeekdaysString week_days[] ={"Monday","Tuesday","Wednesday","Thrusday","Friday"};// Create a combo boxComboBox combo_box =newComboBox(FXCollections.observableArrayList(week_days));// Create a tile paneTilePane tile_pane =newTilePane(combo_box);// Create a sceneScene scene =newScene(tile_pane,200,200);// Set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// Launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- Program to create a combo box and add an event handler to it: This program creates a ComboBox named combo_box and add a list of string to it using(ChoiceBox(FXCollections.observableArrayList(week_days))). We would add the combo box and a label(description) to the tilepane(getChildren().add() function). We will create a stage (container) and add the tilepane to the scene and add the scene to the stage. we would display the stage using show() function. We would add an event handler event to handle the events of combo_box which will change the text of the label selected to the item selected. we will also add the label selected to the tile pane.
// Java program to create a combo box and add event handler to itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjavafx.stage.Stage;importjavafx.scene.text.Text.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;publicclasscombo_box_2extendsApplication {// Launch the applicationpublicvoidstart(Stage stage){// Set title for the stagestage.setTitle("creating combo box ");// Create a tile paneTilePane r =newTilePane();// Create a labelLabel description_label =newLabel("This is a combo box example ");// WeekdaysString week_days[] ={"Monday","Tuesday","Wednesday","Thrusday","Friday"};// Create a combo boxComboBox combo_box =newComboBox(FXCollections.observableArrayList(week_days));// Label to display the selected menuitemLabel selected =newLabel("default item selected");// Create action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){selected.setText(combo_box.getValue() +" selected");}};// Set on actioncombo_box.setOnAction(event);// Create a tile paneTilePane tile_pane =newTilePane(combo_box, selected);// Create a sceneScene scene =newScene(tile_pane,200,200);// Set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// Launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:


Note: The above programs might not run in an online IDE please use an offline converter.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ComboBox.html
Recommended Posts:
- JavaFX | Arc with examples
- JavaFX | Box with examples
- JavaFX | Cylinder with examples
- JavaFX | Sphere with examples
- JavaFX | QuadCurve with examples
- JavaFX | Button with examples
- JavaFX | RadioButton with examples
- JavaFx | ColorPicker with examples
- JavaFX | CubicCurve with examples
- JavaFX | ContextMenu with examples
- JavaFX | CheckMenuItem with examples
- JavaFX | PointLight with examples
- JavaFX | Polyline with examples
- JavaFX | Circle with examples
- JavaFX | Polygon with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet



