Skip to main content

Beans binding

4 replies [Last post]
khandes3
Offline
Joined: 2004-10-30

Hi
I started playing about with the beans binding functionality in Netbeans today.
I have a JXTable component which binds to an ArrayList. (All auto-generated code by the IDE). The problem is that I don't know how to add / remove data from the arraylist and have the changes reflect in the JXTable correctly.

I start off with an empty list and I call a method which adds objects to the list.
Note that I add this data outside the Swing Event Dispatching Thread. When I call the add method, the add operation completes but then I get a very long exception stack trace in the EDT. (see below).

I tried googling for some binding examples but they seem to be just reading data out from databases. Any help will be much appreciated.

Thanks

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:546)
at java.util.ArrayList.get(ArrayList.java:321)
at org.jdesktop.swingbinding.impl.ListBindingManager$ColumnDescriptionManager.validateBinding(ListBindingManager.java:191)
at org.jdesktop.swingbinding.impl.ListBindingManager.valueAt(ListBindingManager.java:99)
at org.jdesktop.swingbinding.JTableBinding$BindingTableModel.getValueAt(JTableBinding.java:713)
at org.jdesktop.swingx.JXTable.getValueAt(JXTable.java:1397)
at javax.swing.JTable.prepareRenderer(JTable.java:3907)
at org.jdesktop.swingx.JXTable.prepareRenderer(JXTable.java:3518)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2070)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1972)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1895)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
at javax.swing.JComponent.paintComponent(JComponent.java:742)
at javax.swing.JComponent.paint(JComponent.java:1005)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JSplitPane.paintChildren(JSplitPane.java:1021)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4970)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
at javax.swing.JComponent.paint(JComponent.java:995)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1709)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
at java.awt.Component.dispatchEventImpl(Component.java:4031)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
psecheresse
Offline
Joined: 2003-09-02

I could reproduce the same error while I was modifying the wrapped list instead of the observable list. For example:
private final List list = new ArrayList();
private final List sites = ObservableCollections.observableList(list);

If I clear and add a new list into the wrapped list:
list.clear();
list.addAll(aFreshNewList);
--> Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: ...

To keep the JTable (or JXTable) in sync, the change must be applied to the observable collection so the events will propagate the change to the table:
private final List sites = ObservableCollections.observableList(new ArrayList());
[...]
sites.clear();
sites.addAll(aFreshNewList);

flange101
Offline
Joined: 2009-02-03

Check out the http://javagoldmine.site90.net/
There are several chapters on beans plus
anything you can imagine on Java.

khandes3
Offline
Joined: 2004-10-30

Never mind folks. I figured out that I need to wrap my ArrayList in a org.jdesktop.observablecollections.ObservableList so that the changes can be picked up by the JXTable.

Thanks

ezhangin
Offline
Joined: 2008-06-18

Can you elaborate more upon your solution? Once I get about 600-700 entires in my table I get that Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 error message, except mine says Index: 0,Size: 0.

If i start deleting entires however, those numbers change. It seems to take an awful long time to refresh/update the table once the error sets in though.

I have this line near the end (I used the Netbeans example app):

list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());

Message was edited by: ezhangin