944,142 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 880
  • Java RSS
Oct 2nd, 2007
0

counter on a button

Expand Post »
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:
java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
claudiu_is is offline Offline
40 posts
since Jul 2007
Oct 2nd, 2007
0

Re: counter on a button

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).
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 2nd, 2007
0

Re: counter on a button

You could use the following for the countdown
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  1. Timer koTimer = new Timer(1000, new ButtonCountdown(ko, 5));
  2. koTimer.start();
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 4th, 2007
0

Re: counter on a button

thanks for the help Ezzaral
Reputation Points: 10
Solved Threads: 1
Light Poster
claudiu_is is offline Offline
40 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Applet and i/o
Next Thread in Java Forum Timeline: password Authentication without database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC