ListCell我正在嘗試為 JavaFX創建一個非常簡單的自定義Listview。運行應用程序時,有一些項目沒有ListView任何內容。它們是可點擊的,但是是空的。如下圖所示:我正在使用 JDK 12 和基于 Maven JFX Archetype(archetype-artifactid:javafx-archetype-fxml, groupid:org.openjfx) 的設置。我遵循了名為“Turais Custom ListCell”的指南。App.javapublic class App extends Application {private static Scene scene;@Overridepublic void start(Stage stage) throws IOException { scene = new Scene(loadFXML("primary")); stage.setScene(scene); stage.show();}static void setRoot(String fxml) throws IOException { scene.setRoot(loadFXML(fxml));}private static Parent loadFXML(String fxml) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml")); return fxmlLoader.load();}public static void main(String[] args) { launch();}}PrimaryController.javapublic class PrimaryController implements Initializable {ObservableList<Student> students;@FXMLprivate ListView<Student> listView1;@FXMLprivate void switchToSecondary() throws IOException { App.setRoot("secondary");}public PrimaryController(){ super(); students = FXCollections.observableArrayList(); students.add(new Student());}@Overridepublic void initialize(URL url, ResourceBundle resourceBundle) { listView1.setItems(students); listView1.setCellFactory(new Callback<ListView<Student>, ListCell<Student>>() { @Override public ListCell<Student> call(ListView<Student> listView) { return new StudentListViewCell(); } });}}我希望有一個ListCell中間有一個標簽,上面寫著“rightText”的單曲。但似乎我的自定義列表單元格甚至沒有呈現。有什么建議么?
1 回答

元芳怎么了
TA貢獻1798條經驗 獲得超7個贊
除了您的實現看起來比需要的復雜得多之外,您的實際問題非常簡單。
您的ListCell.fxml文件未將 分配fx:id給您的AnchorPane. 因此,當您在StudentListViewCell.java類中定義它時,您實際上是在創建一個新的AnchorPane且不包含您的Label.
只需更改ListCell.fxml文件并分配 id 即可解決該問題:
<AnchorPane fx:id="anchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="label1" layoutX="293.0" layoutY="190.0" text="Label"/>
</children>
</AnchorPane>
添加回答
舉報
0/150
提交
取消