counter on a button

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 31
Reputation: claudiu_is is an unknown quantity at this point 
Solved Threads: 1
claudiu_is's Avatar
claudiu_is claudiu_is is offline Offline
Light Poster

counter on a button

 
0
  #1
Oct 2nd, 2007
I have a Dialog window with a Button ko and a JLabel l. The button`s label refreshes at 1000ms from 5 to 1. There are 2 problems: the JLabel l it`s not displayed before the counter starts, it`s displayed after that, and the button doesn`t work during the countdown, if i push it during the countdown the window closes(thats the function if the ko button) but only after the countdown finishes. This is the piece of code:
  1. public AskWindow(Frame parinte, boolean modala){
  2. super(parinte, modala);
  3. JLabel l;
  4. int t = 5;
  5. this.addWindowListener(new WindowAdapter(){
  6. public void windowClosing(WindowEvent e){
  7. MyMain.raspuns = null;
  8. dispose();
  9. }
  10. });
  11. Panel panel = new Panel();
  12. ko = new Button("OK");
  13. l = new JLabel();
  14. l.setText("ati castigat de "+ nrJocT + " ori");
  15. panel.add(l);
  16. panel.add(ko);
  17. add(panel, BorderLayout.SOUTH);
  18. setLocationRelativeTo(parinte);
  19. pack();
  20. ko.addActionListener(this);
  21. setVisible(true);
  22. for (int i=0; i < t; i++)
  23. {
  24. ko.setLabel("" + (t-i));
  25. try{
  26. Thread.sleep(1000);
  27. }
  28. catch(InterruptedException o){}
  29. }
  30. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: counter on a button

 
0
  #2
Oct 2nd, 2007
You need to set up a Swing Timer to handle the countdown on the button. See the following on timers:How to Use Timers.

The behavior you are getting right now is because you are sleeping in the thread that is creating the UI. Create the UI first and after it is set visible, start the Timer task that updates the label on the button (and it's behavior if needed).
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: counter on a button

 
0
  #3
Oct 2nd, 2007
You could use the following for the countdown
  1. class ButtonCountdown implements ActionListener {
  2. int count=0;
  3. JButton button = null;
  4.  
  5. public ButtonCountdown(final JButton button, int count){
  6. this.count = count;
  7. this.button = button;
  8. }
  9. public void actionPerformed(ActionEvent e) {
  10. if (count>0){
  11. button.setText(String.valueOf(--count));
  12. } else {
  13. button.setText("Done!");
  14. }
  15. }
  16. }
and then start it like so after you have the UI all set up
  1. Timer koTimer = new Timer(1000, new ButtonCountdown(ko, 5));
  2. koTimer.start();
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 31
Reputation: claudiu_is is an unknown quantity at this point 
Solved Threads: 1
claudiu_is's Avatar
claudiu_is claudiu_is is offline Offline
Light Poster

Re: counter on a button

 
0
  #4
Oct 4th, 2007
thanks for the help Ezzaral
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC