开发者

Detect Updates To Entities Bound to an (editable) JTable

What is the appropriate method to detect changes made to an entity through an editable cell in a JTable? I would like to get the entity which the changes occurred so that I can commit the changes using JPA (eclipselink).

So far I tried a TableModelListener implementation. However, it seems that the TableModelEvent does not provide a reference to the underlying entity (or data provider of the 开发者_高级运维table) object but only to the value (Object) which has changed.

Having a background in AS3, I'm used to writing event driven code. But, what is the right approach to implement this in Java?


Either inspect the row of the TableModelEvent fired by the modification, and ask your model the entity at this row, or fire another dedicated event when a cell is updated, containing the updated entity.

See the Swing tutorial on tables for how to create your own table model, extending AbstractTableModel. It's almost always better than using the default one, and I thought you were doing it already, because you said that "changes [were] made to an entity through an editable cell".


It turned out that there is a much simpler way to get the updated entity if the data (entities) is bound to the JTable using a BindingGroup (which is the case in my case):

bindingGroup.addBindingListener( new AbstractBindingListener() {

    @Override
    public void synced(Binding binding) {

        super.synced(binding);

        // the entity object
        Object sourceObject = binding.getSourceObject();   
    } 
}

I'm certain that the same could be achieved using a custom TableModel and may be the proper approach. However, the above implementation is just much easier in my case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜