Sound / JTable在绑定数据触发更改后不会更新 [英] Swing/JTable Not Updating After Bound Data Fires Change

查看:118
本文介绍了Sound / JTable在绑定数据触发更改后不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到我的EventTracker bean的JTable,本质上是一个列表的包装器,我将仅用作附加/清除(即简单的日志)。问题是,当我添加列表中的条目并尝试触发一个事件时,我看不到任何更改。我正在使用NetBeans IDE。

I have a JTable which is bound to my EventTracker bean, essentially a wrapper around a list which I will use as append/clear only (i.e., a simple log). Problem is, when I add entries to the list and try to fire an event I don't see any changes. I'm using the NetBeans IDE.

EventTracker bean被添加到视图中并被实例化为eventTracker1。从那里,我右键单击表并选择表内容...。表模型绑定到eventTracker1,绑定表达式为'$ {eventList}'。列被正确设置以对eventList中的条目进行操作。

The EventTracker bean is added to the view and instantiated as eventTracker1. From there, I right click on the table and choose 'Table Contents...'. Table model is bound to eventTracker1, binding expression is '${eventList}'. The columns are set up properly to operate on the entries in eventList.

// From inside EventTracker.java
public static final String EVENT_LIST_PROPERTY = "eventList"; 
public List getEventList() {
    System.out.println("Handing out eventList with size: " + Integer.toString(eventList.size()));
    return eventList;
}

public void setEventList(List incomingList) {
    List oldList = eventList;
    eventList = new ArrayList(incomingList);
    propertySupport.firePropertyChange(EVENT_LIST_PROPERTY, oldList, eventList);
}

当我的外部代码在setEventList上运行时,firePropertyChange似乎适合specSo,似乎触发了事件,因为那时getEventList被调用,并且列表大小正如预期的那样上升。只是表没有呈现。

The method firePropertyChange seems to fit the specSo when my outside code operates on setEventList, it seems to fire off the event because then getEventList is called and the list size is going up as expected. It's just that the table isn't rendering. What can I do to make this work?

推荐答案

假设您的数据模型来源于 AbstractTableModel ,你可以明确更新您的模型,并触发抽象父级中实现的适当更新方法。此外,更新必须在 EDT 上进行,通常使用的invokeLater()。另请参阅 聆听数据更改 启动数据更改事件

Assuming your data model derives from AbstractTableModel, you can update your model explicitly and fire the appropriate update method implemented in the abstract parent. Moreover, updates must occur on the EDT, typically using invokeLater(). See also Listening for Data Changes and Firing Data Change Events.

EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
        // update model, which should fire the appropriate event
    }
});

这篇关于Sound / JTable在绑定数据触发更改后不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆