JavaFX ComboBox: Set a value to the combo box

A list is a little more complicated, but also a lot more fun to work with, because using JavaFX there is a lot you can do with a list. The class that needs to be instantiated to create a list object is named ComboBox . This class is just one of a bigger family of classes used to create lists, the root class being the abstract class ComboBoxBase. Depending on the desired behavior of the list, if we want support for single or multiple selection, if we want the list to be editable or not, the proper implementation should be chosen. In our case, the ComboBox class matches the requirements: we need a non-editable list, which supports single element section. A ComboBox has a valueProperty() method that returns the current user input. The user input can be based on a selection from a drop-down list or the input manually provided by the user when the list is editable. Let’s see the code to add a list to the top section of the BorderPane and add a listener to record the selected value in the TextArea that we previously declared.

private static String[] data = {"John Mayer", "Frank Sinatra",
   "Seth MacFarlane", "Nina Simone", "BB King", "Peggy Lee"};
...
ComboBox comboBox = new ComboBox();
comboBox.getItems().addAll(data);
borderPane.setTop(comboBox);
comboBox.valueProperty().addListener(
  new ChangeListener() {
    @Override
     public void changed(ObservableValue extends String> observable,
         String oldValue, String newValue) {
         textArea.appendText(newValue + "\n");
      }
});

By default, ComboBox widget do not set a value. If you want to set any default value to ComboBox, use ‘setValue’ method.

For example:

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);
 }
}

Related Post