• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

JavaFX ComboBox: Set a value to the combo box

by admin

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

javafx set the value to combobox

Filed Under: Java

Some more articles you might also be interested in …

  1. Java Basics: Lambda Built-in Functional Interfaces
  2. keytool error java.io.FileNotFoundException: (Access is denied)
  3. Java Loop Control
  4. Java Basics: Overriding Methods, Polymorphism, and Static Classes
  5. User-defined Exceptions in Java
  6. Java Modifier Types
  7. Arrays in Java
  8. Java Applet Basics
  9. Java Character Class
  10. Nested Class in Java

You May Also Like

Primary Sidebar

Recent Posts

  • grpck command – Remove corrupt or duplicate entries in the /etc/group and /etc/gshadow files.
  • xxd command – Expressed in hexadecimal form
  • sesearch: command not found
  • macof: command not found

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright