为了账号安全,请及时绑定邮箱和手机立即绑定

对自定义 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 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信