在 JTable 链接模型上同步删除元素以避免 NullPointerExceptions?

Synchronyzed elements deletion on JTable-linked-model to avoid NullPointerExceptions?

我很确定这个问题有一个简单的解决方案,但我一直没能解决它。

场景:

1) 我有一个 JTable 链接到基于具有(例如)10 个元素的 LinkedHashMap 的模型。

2) 外部事件几乎同时删除前2个元素,所以:

2.1) 模型删除元素 #0,并抛出类似 "row linked to element #0, it is, row #0, has changed".

的事件

2.2) JTable还没来得及修改,model上删除了第二个元素和发起的事件一样,因为第二个元素现在是第一个一.

3) JTable 处理两个事件 "row #0 has been deleted".

我想这就是为什么我得到 "NULL error" 如下所示:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at paneles.panel_positions.Elements_Manager.getValueAt(Elements_Manager.java:388)
    at paneles.panel_positions.Elements_ManagerDates_Renderer.getTableCellRendererComponent(Positions_Manager.java:891)
    at javax.swing.JTable.prepareRenderer(JTable.java:5736)
    at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
    at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JViewport.paint(JViewport.java:731)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5230)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5178)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4989)
    at javax.swing.RepaintManager.run(RepaintManager.java:808)
    at javax.swing.RepaintManager.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
    at javax.swing.RepaintManager.access00(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access0(EventQueue.java:103)
    at java.awt.EventQueue.run(EventQueue.java:694)
    at java.awt.EventQueue.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我一直在考虑任何一种同步机制,以便在第一个删除完成并在 JTable 上执行事件之前无法执行第二个删除,但我无法弄清楚哪个会是这样做的正确方法。

这是在实时事件处理应用程序上发生的,因此强制事件源以这种方式等待似乎不是一个好的解决方案,但我想不出正确的方法正在做。有什么想法吗?

提前致谢。

编辑:

启动删除的另一个线程上的方法:

public void delete_all_elements() {

cloned_model = mainobject.getCloned_model();

for (Element pos_aux : cloned_model.values()) {
        notify_pos_to_be_deleted(pos_aux);
    }
}

在消息队列中接收上一条消息的线程上执行的代码:

...
model.rmv(row_index);
TableModelEvent event = new TableModelEvent (this, row_index, total_rows-1, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
notify_suscriptors(event);
...

notify_suscriptors代码:

...
((TableModelListener)listeners.get(i)).tableChanged(event);
...

您必须在 event dispatch thread. Use EventQueue.invokeLater(), as shown in this related example.

上仅更新您的 Table Model
EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
        // remove row
    }
});