JavaFX: Using FXCollections class to create the list

This entry is part 38 of 55 in the series JavaFX

Under the Collections class, JavaFX provides a FXCollections class that include a few static methods for us to create a list.

import javafx.collections.FXCollections;

According to Oracle, the FXCollections are meant to improve the performance of the methods.

Here we make use of the method called observableArrayList() to create the list.

Notice how the data in the collection is put into the list in line 27:


Here in line 29, I tried to enable multiple selection mode so you could try to figure out what combination of keys is needed to make multiple selections.

package javafxapplication29;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class JavaFXApplication29 extends Application {

    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        ListView<String> lv = new ListView<String>();
        ObservableList<String> data = FXCollections.observableArrayList(
            "Rock", "Country", "Pop", "Rap");


        HBox hboxPane = new HBox(10); // spacing = 10       
        hboxPane.setPadding(new Insets(10, 10, 10, 10));

        Label lb = new Label("Selection:");

        hboxPane.getChildren().addAll(lb, lv);

        Scene scene = new Scene(hboxPane, 450, 300);

    public static void main(String[] args) {


Series Navigation<< JavaFX: Getting selected item from listview
JavaFX: Setting an exact height for list view >>

Leave a comment

Leave a Reply