JavaFX ListView - Fill From HashMap


In this class we see how to populate a JavaFx ListView with data from a HashMap. We also handle the JavaFX ListView's itemClicks thus showing an alert dialog.

1. Introduction to JavaFX

JavaFX is modern open source GUI toolkit first released in the year 2008 for developing rich internet applications(RIAs) in Java.

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

2. 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.

3. Main.java

Here's our main and only class containing our main method.

package info.camposha;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
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.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Observable;

public class Main extends Application {

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

        Label titleLabel = new Label("HashMap 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().add(vbox);
        group.setLayoutX(100);

        stage.setScene(scene);
        stage.show();
    }
    /*
    Populate ListView with HashMap data
     */
    private void populateData()
    {
        HashMap<Integer,String> nebulae =new HashMap<>();
        nebulae.put(1,"Horse Head");
        nebulae.put(2,"Black Widow");
        nebulae.put(3,"Ghost Head");
        nebulae.put(4,"Cat's Eye");
        nebulae.put(5,"Elephant's Trunk");
        nebulae.put(6,"Helix");
        nebulae.put(7,"Rosette");
        nebulae.put(8,"Snake");
        nebulae.put(9,"Bernad 68");
        nebulae.put(10,"Ant");
        nebulae.put(11,"Orion");
        nebulae.put(12,"Butterfly");
        nebulae.put(13,"Eagle");
        nebulae.put(14,"Own");
        nebulae.put(15,"Ring");
        nebulae.put(16,"Pelican");
        nebulae.put(17,"Cone");
        nebulae.put(18,"Flame");
        nebulae.put(19,"Witch Head");
        nebulae.put(20,"Bumerang");

        for (String nebular : nebulae.values())
        {
            mListView.getItems().add(nebular);
        }
    }
    private HashMap<Integer, String> getData()
    {
        HashMap<Integer,String> nebulae =new HashMap<>();
        nebulae.put(1,"Horse Head");
        nebulae.put(2,"Black Widow");
        nebulae.put(3,"Ghost Head");
        nebulae.put(4,"Cat's Eye");
        nebulae.put(5,"Elephant's Trunk");
        nebulae.put(6,"Helix");
        nebulae.put(7,"Rosette");
        nebulae.put(8,"Snake");
        nebulae.put(9,"Bernad 68");
        nebulae.put(10,"Ant");
        nebulae.put(11,"Orion");
        nebulae.put(12,"Butterfly");
        nebulae.put(13,"Eagle");
        nebulae.put(14,"Own");
        nebulae.put(15,"Ring");
        nebulae.put(16,"Pelican");
        nebulae.put(17,"Cone");
        nebulae.put(18,"Flame");
        nebulae.put(19,"Witch Head");
        nebulae.put(20,"Bumerang");

        return nebulae;
    }
    /*
     Handle ListView itemClicks
     */
    private void handleItemClicks()
    {
        mListView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent 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.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post