Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Using thread with GUI  RSS feed

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a piece of java code which is displaying a progress bar when some procedure is going on (writing some data to DB ) in a thread
problem is when  i do use GUI with this logic  i discover that some records are not written to DB and when I use this piece of code without the GUI its working fine.
Can not figure out where is the problem.

  Thread PrcessReceiptsThread = new Thread() {
                public void run() {
                   JFrame frame = new JFrame("Process");
                   JLabel label = new JLabel("Please wait...");
                   JProgressBar pb = new JProgressBar();
                   pb.setBackground(new java.awt.Color(248, 201, 171));
                   frame.setBackground(new java.awt.Color(242, 186, 152));
                   frame.setSize(400, 200);
                   JPanel panel = new JPanel();
                   panel.setBackground(new java.awt.Color(242, 186, 152));
                   label.setBounds(100, 30, 250, 10);
                   pb.setBounds(100, 50, 200, 20);
Posts: 3040
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You basic logic is wrong.

All GUI components should be created and updated on the Event Dispatch Thread (EDT)

Then if you have a long running task (like updating a database) you create a separate Thread for that processing.

So only the writeToDB() method should be executed in the Thread.

Or instead of a Thread you can use a SwingWorker

Read the section from the Swing tutorial on Concurrency in Swing for more information about the EDT and SwingWorker.

The tutorial also has a section on How to Use Progress Bars. I suggest you read that and download the demo code and use it as the starting point for your class. Just replace the "long running task" in the demo with your DB code.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!