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:

  1. ComboBox(): creates a default empty combo box
  2. ComboBox(ObservableList i): creates a combo box with the given items

Commonly used Methods:



method explanation
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 items 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to create a combo Box and add items to it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.collections.*;
    import javafx.stage.Stage;
    import javafx.scene.text.Text.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    public class combo_box_1 extends Application {
      
        // Launch the application
        public void start(Stage stage)
        {
            // Set title for the stage
            stage.setTitle("creating combo box ");
      
            // Create a tile pane
            TilePane r = new TilePane();
      
            // Create a label
            Label description_label =
                         new Label("This is a combo box example ");
      
            // Weekdays
            String week_days[] =
                       { "Monday", "Tuesday", "Wednesday",
                                        "Thrusday", "Friday" };
      
            // Create a combo box
            ComboBox combo_box =
                         new ComboBox(FXCollections
                                     .observableArrayList(week_days));
      
            // Create a tile pane
            TilePane tile_pane = new TilePane(combo_box);
      
            // Create a scene
            Scene scene = new Scene(tile_pane, 200, 200);
      
            // Set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        public static void main(String args[])
        {
            // Launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    Output:

  • 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a combo box and add event handler to it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.collections.*;
    import javafx.stage.Stage;
    import javafx.scene.text.Text.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    public class combo_box_2 extends Application {
      
        // Launch the application
        public void start(Stage stage)
        {
            // Set title for the stage
            stage.setTitle("creating combo box ");
      
            // Create a tile pane
            TilePane r = new TilePane();
      
            // Create a label
            Label description_label =
                             new Label("This is a combo box example ");
      
            // Weekdays
            String week_days[] =
                       { "Monday", "Tuesday", "Wednesday",
                                       "Thrusday", "Friday" };
      
            // Create a combo box
            ComboBox combo_box =
                        new ComboBox(FXCollections
                                  .observableArrayList(week_days));
      
            // Label to display the selected menuitem
            Label selected = new Label("default item selected");
      
            // Create action event
            EventHandler<ActionEvent> event =
                      new EventHandler<ActionEvent>() {
                public void handle(ActionEvent e)
                {
                    selected.setText(combo_box.getValue() + " selected");
                }
            };
      
            // Set on action
            combo_box.setOnAction(event);
      
            // Create a tile pane
            TilePane tile_pane = new TilePane(combo_box, selected);
      
            // Create a scene
            Scene scene = new Scene(tile_pane, 200, 200);
      
            // Set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        public static void main(String args[])
        {
            // Launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    Output:

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



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

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, ManasChhabra2



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.