Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to use the ChoiceBox in JavaFX | 100% Perfect tutorial

August 6, 2022 - Updated on November 2, 2022
in Java
Reading Time: 5 mins read
0
ChoiceBox in JavaFX
457
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • How to use the ChoiceBox in JavaFX
    • How does ChoiceBox work
  • JavaFX ChoiceBox Example
    • Display the ChoiceBox to the Scene Graph
      • Output
    • Adding Items to the ChoiceBox
      • Output
    • Get the selected value
      • Output
    • Allowing null in the list of items
      • Output
    • Selection Model
  • YouTube Video

How to use the ChoiceBox in JavaFX

Using the ChoiceBox in JavaFX is the same as using the JavaFX ComboBox in JavaFX. The JavaFX ChoiceBox is a parameterized class and this node in JavaFX is used for a small list of items. The ComboBox in JavaFX is the advanced version of the JavaFX ChoiceBox, and we will talk about the JavaFX ComboBox in the next tutorial.

The use of the ChoiceBox is to let the user select an item from the list of items declared in the parameter of the ChoiceBox. In this tutorial, you will learn how to use the ChoiceBox. Since you are a beginner, I will walk you through step-by-step creating the ChoiceBox and using it.

How does ChoiceBox work

Before we dive into the tutorial, you must understand how ChoiceBox works when you have a ChoiceBox in your JavaFX application. This control or node has four different states:

  • The first state shows its initial state when there is no selection.
  • The second state is when the node has focused and pressed the arrow keys by selecting an item.
  • The third state shows the selected item and the pop-up menu is collapsed.
  • And the fourth state shows the selected item when the pop-up menu is shown. It displays a checkmark in the pop-up menu.

JavaFX ChoiceBox Example

The following examples below will show you how to create the ChoiceBox, add items to select, get selected item, and teach you many more functionalities using the ChoiceBox in JavaFX. Please proceed below to learn more about ChoiceBox and let’s get started.

If you are using a Scene Builder to create your application, you can easily create the ChoiceBox in your application by simply dragging the node into the scene graph. Other than that, if you are not using the Scene Builder, you can simply initialize the choice box class in your code. Please proceed to the given example code snippet below to learn more about how to create the ChoiceBox in your JavaFX application.

// Create the choicebox in your Java Class.
ChoiceBox cb = new ChoiceBox();

Display the ChoiceBox to the Scene Graph

If you are using the FXML-based JavaFX application or using the SceneBuilder to make your GUI, you can load your fxml file to your Java code using the FXMLLoader and you can watch the video tutorial below to know how to use the FXMLLoader. You must also know how to use the layout in JavaFX. The below example will show you pure Java code to display the JavaFX ChoiceBox in your JavaFX application.

// Display the ChoiceBox in the SceneGraph
ChoiceBox cb = new ChoiceBox();
StackPane layout = new StackPane(cb);
Scene scene = new Scene(layout, 300, 400);
stage.setScene(scene);
stage.show();

Output

JavaFX ChoiceBox Display

Adding Items to the ChoiceBox

Adding items to the ChoiceBox is very easy to do since it is a parameterized class. You can specify the list of items using the Observable List or using the getItems().addAll() method. The following example below will show you how to add items to the ChoiceBox. Please see the following code snippet below.

// using the ObservableList
ObservableList<String> list = FXCollections.<String>observableArrayList("java", "Python","C++","C#");
ChoiceBox<String> cb = new ChoiceBox<>(list);

// using the Items property
ChoiceBox<String> cb = new ChoiceBox<>();
cb.getItems().addAll("java", "Python","C++","C#");

Output

ChoiceBox in JavaFX Items

Get the selected value

Once you have items in your JavaFX ChoiceBox, you must know how to get the selected value when you select an Item from the ChoiceBox. You need to use the getValue() method to get the selected value. The following code snippet below will show you how to get the selected value from the ChoiceBox in JavaFX.

// Getting the selected item
ChoiceBox<String> cb = new ChoiceBox<>();
cb.getItems().addAdd("java", "Python","C++","C#");
cb.setOnAction(this::getData);

// create a method getData(ActionEvent e)
private void getData(){
    String selected = cb.getValue();
    System.out.println(selected);
}

Output

JavaFX ChoiceBox Selected

Allowing null in the list of items

We can also add null to the list, allowing null can be used to say “Nothing selected” or “No selected item“. We can convert the null into a string to say something. Using the string converter can achieve that feature. The code snippet below will show you how to convert the null into a string.

ChoiceBox<String> cb = new ChoiceBox<>();
cb.getItems().addAll(null,"Java","Python","C++","JavaScript");

cb.setConverter(new StringConverter<String>(){
    @Override
    public String toString(String s) { return (s == null) ? "Nothing Selected" : s; }
    
    @Override
    public String fromString(String s) { return null; }
});

Output

ChoiceBox in JavaFX Null

Selection Model

There is so much to learn about the selection model, you can select the first, next, previous, and last item from the ChoiceBox. You can also clear the selection method. This means it clears the selected item and returns to a state that no item had been selected. See the following code snippet below to learn more.

// The first item will be selected
cb.getSelectionModel().selectFirst();

// The next item will be selected
cb.getSelectionModel().selectNext();

// The previous item will be selected
cb.getSelectionModel().selectPrevious();

// The last item will be selected
cb.getSelectionModel().selectLast();

// Clears the selection
cb.getSelectionModel().clearSelection();

Watch the video tutorial below if you want a video-based tutorial. If you want to learn more about JavaFX, you can go here.

YouTube Video

YouTube video
Previous Post

How to install IntelliJ IDEA on Windows 10 and 11 | Perfect

Next Post

How to use the ComboBox in JavaFX | 100% Perfect Tutorial

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
586
Next Post
JavaFX ComboBOx

How to use the ComboBox in JavaFX | 100% Perfect Tutorial

How to use the ListView in JavaFX | 100% Perfect Tutorial

How to use the ListView in JavaFX | 100% Perfect Tutorial

JavaFX ColorPicker

How to use the ColorPicker in JavaFX | 100% Perfect Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.