• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing UI lag while adding rows to database table

 
Justin Vasquez
Greenhorn
Posts: 5
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20746
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There sure is. Tutorial here: Lesson: Concurrency in Swing.
 
Justin Vasquez
Greenhorn
Posts: 5
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20746
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, using a SwingWorker fixed my problem.
I really appreciate it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic