JavaFX - ListView - Fill From ArrayList

March 30, 2018 Oclemy JavaFX ListView, Java ArrayList 3 minutes, 41 seconds

So in this class the aim is to popuate a JavaFX ListView with data from an ArrayList and handle the selected ListView itemClicks thus showing an alert dialog with the selected item.

This will get us working with JavaFX since it's giving us a practical enough example to get a taste of JavaFX especially if you are new.

If your prefer a video tutorial then check here:

What is JavaFX?

JavaFX is modern open source GUI toolkit first released in the year 2008 for developing rich internet applications(RIAs) in Java. It was open sourced in the year 2013 by Oracle.

We can use all the popular JVM based languages to create JafaFX applications, including languages like Java, Kotlin, Groovy and Scala.

JavaFX is richer in terms of functionality as compared to Swing, yet much much easier and requires writing less code as opposed to Swing.

It's the normally viewed as the successor to Swing.

Features of JavaFX

Here are some of the major features of JavaFX.

No. Feature
1. JavaFX provides first class support to data binding.
2. JavaFX support multi-language coding. Any JVM-based scripting language like Groovy and Scala can be used.
3. JavaFX is written in Java. Hence it allows take full advantage f Java features like Generics, multithreading etc.
4. JavaFX provides both imperative and declarative ways to create user interfaces. You can either use Java code or use FXML, an XML-based scripting markup language.
5. Rich internet content can be embedded in JavaFX applications. This is possible due to its web engine. This engine will process web content embedded in a scene graph. This web engine is based in a popular open source web browser engine called WebKit.
6. JavaFX has rich support for multimedia e.g audio and video playbacks. This is because it has an inbuilt media engine which provide this support. This engine will utilize your platforms available codecs.
7. Animations and effects can be applied to JavaFX applications using JavaFX APIs.
8. JavaFX is open source. Oracle open sourced JavaFX in the year 2013. Thefore you can view the source code and see how things work behind the scenes. After installation the source will be copied in the Java Home directory, in a file called javafx-src.zip. You can use editors to view this source code.

Main.java

Here is our main and only class.

As usual with JavaFX we have to start by extending the javafx.application.Application class. It's actually an abstract class so we will then need to override the start() method.

That start() method will take a Stage object which is the primary stage for the application.

Our data source will be an arraylist so we populate it with data.

When an item in our ListView is clicked we will be showing a a JavaFX dialog.

package info.camposha;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.ArrayList;

public class Main extends Application {

    private ListView mListView=new ListView();
    /*
    Construct UI
     */
    @Override
    public void start(Stage stage) throws Exception {
        Scene scene = new Scene(new Group());
        stage.setTitle("JavaFX ListView - Fill From ArrayList");
        stage.setWidth(550);
        stage.setHeight(550);

        Label titleLabel = new Label("List of Nebulas");
        titleLabel.setFont(new Font("Lucida", 20));

        populateData();
        handleItemClicks();

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(titleLabel, mListView);
        vbox.setAlignment(Pos.CENTER);

        Group group=((Group) scene.getRoot());
        group.getChildren().addAll(vbox);
        group.setLayoutX(100);

        stage.setScene(scene);
        stage.show();
    }
    /*
    Populate ListView with ArrayList data
     */
    private void populateData()
    {
        ArrayList<String> nebulae =new ArrayList();
        nebulae.add("Horse Head");
        nebulae.add("Black Widow");
        nebulae.add("Ghost Head");
        nebulae.add("Cat's Eye");
        nebulae.add("Elephant's Trunk");
        nebulae.add("Helix");
        nebulae.add("Rosette");
        nebulae.add("Snake");
        nebulae.add("Bernad 68");
        nebulae.add("Ant");
        nebulae.add("Orion");
        nebulae.add("Butterfly");
        nebulae.add("Eagle");
        nebulae.add("Own");
        nebulae.add("Ring");
        nebulae.add("Pelican");
        nebulae.add("Cone");
        nebulae.add("Flame");
        nebulae.add("Witch Head");
        nebulae.add("Bumerang");

        for (String nebular : nebulae)
        {
            mListView.getItems().add(nebular);
        }
    }
    /*
     Handle ListView itemClicks
     */
    private void handleItemClicks()
    {
        mListView.setOnMouseClicked(event -> {
            String selectedItem = mListView.getSelectionModel().getSelectedItem().toString();
            Dialog d=new Alert(Alert.AlertType.INFORMATION,selectedItem);
            d.show();
        });
    }
    /*
    Main method
     */
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Best Regards, Oclemy.

Comments