• 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 Instance Variables
  2. Java Basics: Abstract and Nested Classes
  3. Using ‘abstract’ Keyword in Java
  4. Basics of Java
  5. Java Jump Statements – break, continue, labeled statement
  6. Scope of Variables in Java
  7. Java Inheritance
  8. Java Field and Method Modifiers
  9. Java Loop Control
  10. Basic Java: Generics and Collections

You May Also Like

Primary Sidebar

Recent Posts

  • Chezmoi: A multi-machine dotfile manager, written in Go
  • cheat: Create and view interactive cheat sheets on the command-line
  • chars: Display names and codes for various ASCII and Unicode characters and code points
  • chafa: Image printing in the terminal

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright