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

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

對自定義 javafx listview 單元格進行故障排除

對自定義 javafx listview 單元格進行故障排除

翻翻過去那場雪 2023-03-23 15:38:53
列表視圖單元格加載空白而不是自定義單元格我試過重建 fxml 文件,使用調試工具,這些都沒有幫助。我在網上廣泛查看但只能找到舊版本 javafx listview 的指南。在有人告訴我 updateItem 代碼錯誤之前,請先幫我讓它工作,然后至少可以從某個地方開始優化它package Test;import java.io.IOException;import javafx.fxml.FXML;import javafx.fxml.FXMLLoader;import javafx.scene.control.Label;import javafx.scene.control.ListCell;import javafx.scene.layout.Pane;import org.controlsfx.glyphfont.Glyph;/** * FXML Controller class * * @author james */public class ListCellController2 extends ListCell<Student>{    FXMLLoader loader;    @FXML    private Glyph attendenceSymbolGlyph;    @FXML    private Label studentDetailsLabel;    @FXML    private Pane entryPane;   @Override   protected void updateItem(Student student, boolean empty)   {       super.updateItem(student, empty);       if (empty || student == null)       {           setText(null);           setGraphic(null);       }       else       {           loader = new FXMLLoader(getClass().getResource("ListCell2.fxml"));           try           {               loader.load();               studentDetailsLabel.setText(student.toString());               setGraphic(entryPane);           }           catch (IOException ex)           {               ex.printStackTrace();           }       }   }}<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.Label?><?import javafx.scene.layout.Pane?><?import org.controlsfx.glyphfont.Glyph?><Pane fx:id="entryPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="95.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Test.ListCellController2">   <children>      <Glyph id="attendenceSymbolGlyph" fx:id="attendenceSymbolGlyph" layoutX="5.0" layoutY="8.0" prefHeight="80.0" prefWidth="87.0" />      <Label id="studentDetailsLabel" fx:id="studentDetailsLabel" layoutX="113.0" layoutY="5.0" prefHeight="88.0" prefWidth="482.0" text="Label" />   </children></Pane>我的預期結果是它將我的數據加載到自定義單元格中然后顯示它,它實際上做的是給我一個空白單元格
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

看起來你創建了你的ListCellController2 類的兩個實例。

  • 您在列表視圖單元工廠中創建第一個實例

  • 第二個實例是在加載 FXML 時創建的,因為在 FXML 中給出了控制器類

我還建議您只在創建單元格時加載列表單元格組件一次,而不是每次顯示新值時都加載一次。

控制器由單元工廠創建,實例由 FXML 加載器使用。這樣,帶注釋的字段@FXML就可以正確初始化。

這是我的代碼:

MainController.java

import java.net.URL;

import java.util.ResourceBundle;


import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.fxml.FXML;

import javafx.fxml.Initializable;

import javafx.scene.control.ListView;


public class MainController implements Initializable {


    @FXML

    public ListView<Student> myListView;

    public ObservableList<Student> studentList;


    public MainController() {

        studentList = FXCollections.observableArrayList();

        studentList.add(new Student("Jimmy", "u0764987", "ef937b3"));

        studentList.add(new Student("John", "u0762809", "543jh32"));

    }


    @Override

    public void initialize(URL location, ResourceBundle resources) {

        if (!studentList.isEmpty()) {

            setupListView();

        } else {

            System.out.println("student list is empty");

        }

    }

    

    private void setupListView() {

        myListView.setItems(studentList);

        myListView.setCellFactory((listView) -> new ListCellController2());

    }

}

ListCellController2

import java.io.IOException;

import org.controlsfx.glyphfont.Glyph;


import javafx.fxml.FXML;

import javafx.fxml.FXMLLoader;

import javafx.scene.control.Label;

import javafx.scene.control.ListCell;

import javafx.scene.layout.Pane;


public class ListCellController2 extends ListCell<Student> {


    @FXML

    private Glyph attendenceSymbolGlyph;

    @FXML

    private Label studentDetailsLabel;

    @FXML

    private Pane entryPane;


    public ListCellController2() {

        try {

            FXMLLoader loader = new FXMLLoader();

            loader.setController(this);

            loader.setLocation(getClass().getResource("ListCell2.fxml"));

            loader.load();

        } catch (IOException e) {

            throw new RuntimeException("Creating UI component failed", e);

        }

    }


    @Override

    protected void updateItem(Student student, boolean empty) {

        super.updateItem(student, empty);


        if (empty || student == null) {

            setText(null);

            setGraphic(null);

        } else {

            studentDetailsLabel.setText(student.toString());

            setGraphic(entryPane);

        }

    }

}

ListCell2.fxml

<?xml version="1.0" encoding="UTF-8"?>


<?import javafx.scene.control.Label?>

<?import javafx.scene.layout.Pane?>

<?import org.controlsfx.glyphfont.Glyph?>


<Pane fx:id="entryPane" prefHeight="95.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">

   <children>

      <Glyph id="attendenceSymbolGlyph" fx:id="attendenceSymbolGlyph" layoutX="5.0" layoutY="8.0" prefHeight="80.0" prefWidth="87.0" />

      <Label id="studentDetailsLabel" fx:id="studentDetailsLabel" layoutX="113.0" layoutY="5.0" prefHeight="88.0" prefWidth="482.0" text="Label" />

   </children>

</Pane>


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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