• Post Reply Bookmark Topic Watch Topic
  • New Topic

time dependent dynamic value change problem  RSS feed

 
kapil Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am showing a value in a JPanel. When I click on a button, a new value should be displayed just for half of second on the panel and after that the original value is displayed. I can't use mouse pressed and released event since the value should return to original even if mouse is kept pressed. If I use sleep method, it doesn't repaint the values and main thread is blocked. Please help.
Thanks,
Kapil
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to Java Ranch!
What you need to do is to do the sleeping and repainting from a separate thread. The javax.swing.Timer class would do a lot of the work for you. Put code comparable to the following in an ActionListener and add it to the JButton:
[code]
changeVariablesToSetJPanelToNewState();
myJPanel.repaint(); // paint new value
ActionListener command = new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeVariablesToSetJPanelBackToOriginalState();
myJPanel.repaint(); // paint old value
}
};
// Create a timer that fires only once, in 500 ms
Timer t = new Timer(500, command);
t.setRepeats(false);
t.start();
 
kapil Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest,
Your answer solved my problem. I was lost in creating and managing separate thread for repainting.
Thanks once again,
Kapil
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!