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

仍然无法理解为什么我的 TableView 更改侦听器不起作用

仍然无法理解为什么我的 TableView 更改侦听器不起作用

跃然一笑 2021-09-15 10:43:27
我知道我的问题是微不足道的,解决方案是表面上的,但我仍然无法处理它,所以需要帮助。问题是我的代码中的 TableView ListChangeListener 没有显示更新的更改。在问一个问题之前,我已经阅读了很多主题、手册和示例但仍然无法理解为什么我的代码不起作用。我已经添加,extractor但它没有帮助我。
查看完整描述

1 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

在寻找对我的问题的评论和缺点后,我发现我需要自己在代码中搜索问题。希望有人会发现这个问题并回答有用。问题出在我的阅读方法中readDataFromStream()。在阅读上述主题之前,我使用了这种方式声明ObservableList


public static ObservableList<EmailData> emailData = FXCollections.observableArrayList(); 

我的readDataFromStream样子:


public static ObservableList<EmailData> readDataFromStream(byte[] bytes) {

    ObservableList<EmailData> emailData = FXCollections.observableArrayList();

    try {

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

        ObjectInputStream ois = new ObjectInputStream(bis);

        List<EmailData> list = (List<EmailData>) ois.readObject();

        emailData = FXCollections.observableList(list);

        bis.close();

        ois.close();

    } catch (ClassNotFoundException | IOException e) {

        e.printStackTrace();

    }

    return emailData;

}

然后我读到我需要使用提取器创建列表。我将ObservableList声明更改为:


ObservableList<EmailData> emailData = FXCollections.observableArrayList(ed ->

        new Observable[]{

                ed.nameProperty(),

                ed.loginProperty(),

                ed.passwordProperty(),

                ed.commentsProperty()

        });

但我的readDataFromStream()方法保持不变。现在我把它改成:


public static ObservableList<EmailData> readDataFromStream(byte[] bytes) {

ObservableList<EmailData> emailData = FXCollections.observableArrayList(ed ->

        new Observable[]{

                ed.nameProperty(),

                ed.loginProperty(),

                ed.passwordProperty(),

                ed.commentsProperty()

        });

    try {

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

        ObjectInputStream ois = new ObjectInputStream(bis);

        List<EmailData> list = (List<EmailData>) ois.readObject();

            emailData = FXCollections.observableList(

        list,

        (EmailData tp) -> new Observable[]{tp.nameProperty(), tp.passwordProperty(),

                tp.loginProperty(), tp.commentsProperty()});

        bis.close();

        ois.close();

    } catch (ClassNotFoundException | IOException e) {

        e.printStackTrace();

    }

    return emailData;

}

和wasUpdated()方法ListChangeListener.Change 现在就像一个魅力。


希望对我这样的新手有所帮助。


查看完整回答
反对 回复 2021-09-15
  • 1 回答
  • 0 关注
  • 150 浏览

添加回答

举报

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