Home > Java > javaTutorial > body text

How to Display Custom Objects in a JavaFX ListView?

Patricia Arquette
Release: 2024-10-24 18:47:55
Original
401 people have browsed it

How to Display Custom Objects in a JavaFX ListView?

Populating a ListView with Custom Objects in JavaFX

When working with JavaFX, populating a ListView with custom objects requires a slightly different approach compared to using Strings. This article will provide a detailed solution for this issue.

Problem Statement

When using an ObservableArrayList of custom objects in JavaFX, the ListView displays the objects themselves as Strings, rather than extracting and displaying the desired property (e.g., a word from a Word object).

Solution

The solution is to use a Cell Factory in the ListView. A Cell Factory allows you to customize the presentation of each cell in the ListView.

Step 1: Create a Cell Factory

Create a new class that extends ListCell. In this example, we will call it WordCell.

public class WordCell extends ListCell<Word> {

    @Override
    protected void updateItem(Word item, boolean empty) {
        super.updateItem(item, empty);

        if (empty || item == null || item.getWord() == null) {
            setText(null);
        } else {
            setText(item.getWord());
        }
    }
}
Copy after login

Step 2: Set the Cell Factory in the ListView

Assign the WordCell class as the cell factory for the ListView.

ListView<Word> listViewOfWords = new ListView<>(wordsList);
listViewOfWords.setCellFactory(listViewOfWords.new WordCell());
Copy after login

Sample Application

Here is a sample application that demonstrates the described approach:

import javafx.application.Application;
import javafx.collections.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class ListViewCustomObject extends Application {

    @Override
    public void start(Stage stage) {
        ObservableList<Word> wordsList = FXCollections.observableArrayList();
        wordsList.add(new Word("First Word", "Definition of First Word"));
        wordsList.add(new Word("Second Word", "Definition of Second Word"));
        wordsList.add(new Word("Third Word", "Definition of Third Word"));

        ListView<Word> listViewOfWords = new ListView<>(wordsList);
        listViewOfWords.setCellFactory(listViewOfWords.new WordCell());

        stage.setScene(new Scene(listViewOfWords));
        stage.show();
    }

    public static class Word {
        private final String word;
        private final String definition;

        public Word(String word, String definition) {
            this.word = word;
            this.definition = definition;
        }

        public String getWord() {
            return word;
        }

        public String getDefinition() {
            return definition;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Copy after login

By following these steps, you can populate your ListView with custom objects and display the desired properties within each cell.

The above is the detailed content of How to Display Custom Objects in a JavaFX ListView?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!