Solved

Threding issue when populating a table

Posted on 2006-11-08
8
299 Views
Last Modified: 2008-02-01
Hi,

I have been testing SwingWorker to gain more performance to our app. I use SwingWorker when I
need to update a table from database. In some cases it is working perfectly, but in some it is giving
me an exception.

Here is what I do:

When a window is opened or a value in combo is changed, etc. I need to update table from database.
With SwingWorker I can do this in another thread (than EDT), so GUI remains usable. I'll start the
update like following:

        SwingWorker sw = new SwingWorker() {
            protected Object doInBackground() throws Exception {
                updateTable();
                return null;
            }
        };
        sw.execute();

I have tried to add fireTableDataChanged(); to my table model and to ignore repaint for the table while updateing.

/ mika
0
Comment
Question by:JamixOy
  • 4
  • 4
8 Comments
 

Author Comment

by:JamixOy
ID: 17904596
Forgot to include the Exception..

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 0
      at java.util.Vector.elementAt(Vector.java:432)
      at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:280)
      at javax.swing.plaf.basic.BasicTableHeaderUI.paint(BasicTableHeaderUI.java:337)
      at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
      at javax.swing.JComponent.paintComponent(JComponent.java:740)
      at javax.swing.JComponent.paint(JComponent.java:1003)
      at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4930)
      at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4883)
      at javax.swing.JComponent._paintImmediately(JComponent.java:4826)
      at javax.swing.JComponent.paintImmediately(JComponent.java:4633)
      at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:451)
      at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:114)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
      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)

/ mika
0
 
LVL 92

Expert Comment

by:objects
ID: 17904603
you can't update your table from a different thread. table updates need to be done from the EDT
0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 17904605
use EventQueue.invokeLater() & invokeAndWait() to do the updates from the EDT
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:JamixOy
ID: 17904642
Hi Objects,

invokeLater() is working ok, but I guess the GUI locks if a long query is issued from the database?

I have created a component that looks something like a table, but it uses javax.swing.JLists and it is updateing ok in a another thread. Is there some danger using SwingWorker like this?

/ mika
0
 
LVL 92

Expert Comment

by:objects
ID: 17904674
> but I guess the GUI locks if a long query is issued from the database?

it will, the long query should be done in seperate thread.
0
 

Author Comment

by:JamixOy
ID: 17904702
Ok, so there is a little designing issue here.. :)

Thanks, Objects.
0
 

Author Comment

by:JamixOy
ID: 17904799
Hi Objects,

Changed my code like this:

        SwingWorker sw = new SwingWorker() {
            protected Object doInBackground() throws Exception {
                fetchDataFromDatabase();
                return null;
            }
            public void done() {
                updateTableWithNewData();
            }
        };
        sw.execute();

This looks like working ok.

/mika
0
 
LVL 92

Expert Comment

by:objects
ID: 17904914
that looks better :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question