Solved

Threding issue when populating a table

Posted on 2006-11-08
8
298 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

832 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