自訂物件如何在 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()); } } });</word>
透過提供自訂單元工廠,您可以指定 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); } }</word></word></word>
此增強的解決方案滿足您使用自訂物件填充 ListView 並同時顯示這些物件的特定屬性的要求。
以上是如何在 JavaFX 中使用自訂物件在 ListView 中顯示特定物件屬性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
3 週前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
2 週前ByDDD
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

禪工作室 13.0.1
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境