Open In App

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() 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:






// 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: 




// 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 


Article Tags :