• 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Repainting JPanel

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just started learning Swing.I'm trying to write a program in which there will be 2 panels.Panel1->for stopwatch Panel2->which will be displaying some data and it contains a button.

My concern is I'm using Threading to update the timer of stopwatch every second. After putting print statement i can see the stop watch part is running fine but the stopwatch panel is not getting updated every second.

Below is the some portion of my code relevant to this problem.

Can anybody help me out plzzzzzzzz!!!

PanelTop is the Class for stopwatch panel

Please let me know if my approach should be something else so as to achieve the requirement
Thanks in advance
 
Marshal
Posts: 71097
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using one thread to start the StopWatch, and you are querying it in another thread. It is worthwhile checking the interval between successive queries, because I think those intervals are too short for repainting.

If you have just started learning Swing, you might do well to try simpler examples. And remember that Swing components are not thread-safe.
 
Bhakta Pradhan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I solved this , actually I was not updating the timer label inside run method of PanelTop.java


Hi Campbell,

Thanks for spending some time to look into this problem.

Actually the Stopwatch code goes below ,I don't think it could be a problem in this scenario. What you say???


 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to respect Swing's single threaded rule.
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html
http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html
 
Sheriff
Posts: 22042
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you take a look at javax.swing.Timer (for use within GUIs) and java.util.Timer (for anywhere else).
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic