Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing and threads  RSS feed

 
Todd Johnson
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been writing java in the web environment for a while, but recently had a chance to play around with swing.


I have a small app that when the user clicks a button, it goes out and runs a database query. This query can run for a relatively long time (30 seconds). During that time I don't want the user doing anything so I disable my buttons and set the cursor for the JFrame to WAIT_CURSOR.

So in pseudocode my Action Listener for the button looks like this...

// Disbale the button and set the cursor to a wait cursor.
myButton.setEnabled(false);
jFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

// Run the query this can take 30 seconds
query.execute();

// Enable the button and set the cursor back
myButton.setEnabled(false);
jFrame.setCursor(null);


The problem I am having, is that the screen doesn't appear to redraw while the query is executing. I'm assuming this is because query.execute() is running on the Swing thread?

What would be the right way to do this?
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup - you got it, you shouldn't run any long running task in the event thread. You can just create your own thread using Thread or Runnable - but I suggest looking at SwingWorker - it provides some nice things like a finished method that runs back in the event thread - to update GUI components. As you cannot safely use GUI components outside of the event thread (yeah realized components - but its best to keep it simple ).

You should definately read Sun's How to Use Threads tutorial
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I'm assuming this is because query.execute() is running on the Swing thread?

yes, it would be blocking the EDT which also handles painting.

run your query via a separate thread, and from this thread (when finished)
update the gui via SwingUtilities (puts it back on the EDT)
 
Todd Johnson
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response guys! With your direction I got it working correctly by extending the SwingWorker class. Although I wonder why they don't include that class in the java libraries.

Thanks again!
Todd
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are adding it in Java 6.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!