Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Repainting JPanel  RSS feed

 
Bhakta Pradhan
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
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • 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???


 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • 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
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java 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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!