Solved

Threding issue when populating a table

Posted on 2006-11-08
8
296 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

8 Experts available now in Live!

Get 1:1 Help Now