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 ListView in JavaFX | 100% Perfect Tutorial

August 21, 2022 - Updated on November 2, 2022
in Java
Reading Time: 5 mins read
0
Listview in JavaFX Thumbnail
499
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • ListView in JavaFX Tutorial
  • How to use the ListView in JavaFX
    • Create the JavaFX ListView
    • Add items to the ListView
    • Display the ListView to the Scene Graph
      • Output
    • Determine the selected value
      • Output
    • Enable the multiple items to be selected
      • Output
    • ListView Orientation
    • ListView Selection Model
  • YouTube Video

ListView in JavaFX Tutorial

If you know the JavaFX ComboBox, the pop-up menu in the ComboBox is the same scenario as the ListView in JavaFX. The JavaFX ListView allows the user to select one or multiple items from a list of items. ComboBox and JavaFX ChoiceBox are almost the same code as the ListView; it is easy to use and learn.

List view in JavaFX is useful when your application requires a list of items to choose from the user, it could be a single or multiple selections. This node or control has a selection model that stores the state of every selected item from the ListView. It has a selection Model property that is used to find the selection model. You can configure the selection modes by using the:

  • Single Selection Mode
  • and the Multiple Selection Mode.

It is easy to understand the selection modes; the Single Selection Mode is you can only select one item at a time. Otherwise, the Multiple Selection Mode can select multiple items at the same time by pressing and holding the CTRL key on the keyboard and clicking the items at the same time. The ListView in JavaFX does have more features to learn, but this tutorial will guide you through the basics or somewhat fundamentals for you to start learning the ListView in JavaFX.

How to use the ListView in JavaFX

The JavaFX ListView is very easy to learn and use. In this tutorial, you will learn how to create the list view, add items to the list view, and get the selected item, and will show you more features in the following examples below. This tutorial will assure you that you can learn something new here as a beginner. Please proceed below to learn more about the List View in JavaFX.

Create the JavaFX ListView

It is very easy to create the List view. You need to create an object from the list view by instantiating the ListView class. The code snippet below will be the instantiation of the ListView.

ListView<String> listView = new ListView();

Use the Scene Builder application to create the ListView easily. You can easily drag and drop the nodes or components. You can also watch the video tutorial below to learn more.

Add items to the ListView

To add items to the JavaFX list view, you need to use the getItems() method and addAdd() or addAll() methods. There are ways to add items to the ListView in JavaFX. The following code snippet will show you the methods of adding items to the list view. Please proceed below to learn more.

// First method
String[] items = {"Java","C#","PHP","Python","JavaScript"};
ListView<String> listView = new ListView<String>();
listView.getItems().addAll(items);

// second method
listView.getItems().add("Single Item");

// third method
listView.getItems().addAll("Item 1","Item 2","Item 3");

Display the ListView to the Scene Graph

You need to add the list view to the Scene Graph to make it visible to the user. You will need the JavaFX Layout to wrap the node and add it to the scene. If you don’t know how to use the JavaFX Layout, you can click the link to learn more. The following example code below will teach you how to make your list view visible in your application.

// this example is not using the scene builder
StackPane layout = new StackPane();
Scene scene = new Scene(layout, 400, 400);

ListView<String> list = new ListView<String>();
layout.getChildren().add(list);
stage.setScene(scene);
stage.show();

Output

JavaFX ListView SceneGraph

Determine the selected value

Getting the selected value when you select an item from the JavaFX ListView is very easy. You need to its selectedItemProperty() and addListener() to make it work. The following example below will show you how to get the selected item.

// inside the initialize method
listView.getSelectionModel().selectedItemProperty().addListener(this::selectionChanged);

// make another method as selectionChanged
private void selectionChanged(ObservableValue<? extends String> Observable, String oldVal, String newVal){
    ObserVableList<String> selectedItems = listView.getSelectionModel().getSelectedItems();
    String getSelectedItem = (SelectedItems.isEmpty())?"No selected Item":selectedItems.toString();
    System.out.println(getSelectedItem);
}

Output

ListView in JavaFX getSelectedItem

Enable the multiple items to be selected

The example code above is for multiple items and the multiple items are disabled by default because it is using the single selection mode. The multiple items mean you can select many or multiple items in the JavaFX List View. To allow the multiple selection mode, you need to use the setSelectionMode() method via getSelectionModel(). The following example below will show you how to allow the multiple-selection mode.

//Allowing the multiple selection mode
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

Output

JavaFX List view multiple items

ListView Orientation

You can also set the JavaFX List View, changing the orientation of the List View depending on your application requirements. You can set it to horizontal or vertical orientation. If you set the orientation to Horizontal, your list of items will move from left to right. Otherwise, top to bottom if you set the orientation to vertical. The following example code below will show you how to change the JavaFX ListView orientation.

// Arrange the list to horizontal
listView.setOrientation(Orientation.HORIZONTAL);

// Arrange the list to Vertical
listView.setOrientation(Orientation.VERTICAL);

ListView Selection Model

You can select items in the list view using buttons or whatever nodes you want to use to select or clear selection. Using the getSelectionModel() method you can achieve this kind of feature. Watch the YouTube video below to learn more.

// use these in the Action event

listView.getSelectionModel().clearSelection();
listView.getSelectionModel().selectAll();
listView.getSelectionModel().selectFirst();
listView.getSelectionModel().selectLast();
listView.getSelectionModel().selectNext();
listView.getSelectionModel().selectPrevious();

YouTube Video

YouTube video
Previous Post

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

Next Post

How to use the ColorPicker 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 ColorPicker

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

How to use the DatePicker in JavaFX | 100% Free Tutorial

How to use the DatePicker in JavaFX | 100% Free Tutorial

JavaFX TextField

6 JavaFX Text Field Examples Perfect for Beginners

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.