首頁  >  文章  >  Java  >  如何在 JavaFX 中使用自訂物件在 ListView 中顯示特定物件屬性?

如何在 JavaFX 中使用自訂物件在 ListView 中顯示特定物件屬性?

Barbara Streisand
Barbara Streisand原創
2024-10-25 05:22:02790瀏覽

How to Display Specific Object Properties in a ListView With Custom Objects in JavaFX?

自訂物件如何在 JavaFX 中填充 ListView

在 JavaFX 中,可以使用 ObservableList 向 ListView 填充資料。使用自訂物件時,當 ListView 顯示物件本身而不是物件的特定屬性時,會出現一個常見的挑戰。

要解決此問題,您可以利用 單元工廠。操作方法如下:

listViewOfWords.setCellFactory(param -> new 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());
        }
    }
});

透過提供自訂單元工廠,您可以指定 ListView 如何呈現每個項目。在本例中,我們指示 ListView 顯示每個 Word 物件的 wordString 屬性,提供所需的輸出。

這是一個完整的範例應用程式:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage stage) {
        // Create a list of Word objects
        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"));

        // Create a ListView instance
        ListView<Word> listView = new ListView<>(wordsList);

        // Set the cell factory to display the wordString property
        listView.setCellFactory(param -> new 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());
                }
            }
        });

        // Create a scene to display the ListView
        VBox layout = new VBox(10);
        layout.setPadding(new Insets(10));
        layout.getChildren().add(listView);

        Scene scene = new Scene(layout);
        stage.setScene(scene);
        stage.show();
    }

    public static class Word {
        private String word;
        private 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);
    }
}

此增強的解決方案滿足您使用自訂物件填充 ListView 並同時顯示這些物件的特定屬性的要求。

以上是如何在 JavaFX 中使用自訂物件在 ListView 中顯示特定物件屬性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn