Open In App

JavaFX | ComboBox with examples

Last Updated : 20 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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() This method gets the value of the property editor
getItems() This method returns the items of the combo box
getVisibleRowCount() This method returns the value of the property visibleRowCount.
setItems(ObservableList v) This method Sets the items of the combo box
setVisibleRowCount(int v) This method 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




// 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",
                                    "Thursday", "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);
    }
}


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.

Java




// 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",
                                   "Thursday", "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);
    }
}


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 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads