亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaFx自定義ListCell為空

JavaFx自定義ListCell為空

胡說叔叔 2023-07-28 09:41:16
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>


查看完整回答
反對 回復 2023-07-28
  • 1 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號