Solved

Threding issue when populating a table

Posted on 2006-11-08
8
297 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 70
JAVA part two 5 55
unix in java example 9 52
micro services vs rest web services 16 81
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now