Solved

Threding issue when populating a table

Posted on 2006-11-08
8
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven disable workspace resolution 1 79
printf performancy 11 105
Netbeans and org.apache.commons.lang3 issue 3 55
maximize the sum of fractions 33 76
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…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

751 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