aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing UI lag while adding rows to database table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing UI lag while adding rows to database table" Watch "Swing UI lag while adding rows to database table" New topic
Author

Swing UI lag while adding rows to database table

Justin Vasquez
Greenhorn

Joined: Jul 05, 2011
Posts: 5

I have a program that takes in log files, parses them, and inserts rows to a database that it connects to on startup. The GUI consists of two JButtons (one for importing and one for displaying data) and a JLabel to display the status.
Normally the GUI updates fine all the time (JLabel text change and disabling buttons), but when the program is inserting rows into the table, the whole GUI freezes until the operation completes, skipping all the UI changes that I make.
Is there any way to update the UI while I'm accessing the database?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There sure is. Tutorial here: Lesson: Concurrency in Swing.
Justin Vasquez
Greenhorn

Joined: Jul 05, 2011
Posts: 5

Thanks for the quick and useful reply.
I've gone through that, and I'm having little but of trouble understanding how to implement this. I currently have a main class that has the GUI and action listeners, and another that handles the database, including connecting and inserting.
Would the database class be a SwingWorker?
What do you recommend that I should do/how do you think I should implement it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No. You should use the SwingWorker to run the code which updates the database. Your "database class" should have a method which does that, so that the SwingWorker can just call that method.
Justin Vasquez
Greenhorn

Joined: Jul 05, 2011
Posts: 5

Thank you very much, using a SwingWorker fixed my problem.
I really appreciate it.
 
Don't get me started about those stupid light bulbs.
 
subject: Swing UI lag while adding rows to database table