| | |
counter on a button
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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)
public AskWindow(Frame parinte, boolean modala){ super(parinte, modala); JLabel l; int t = 5; this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ MyMain.raspuns = null; dispose(); } }); Panel panel = new Panel(); ko = new Button("OK"); l = new JLabel(); l.setText("ati castigat de "+ nrJocT + " ori"); panel.add(l); panel.add(ko); add(panel, BorderLayout.SOUTH); setLocationRelativeTo(parinte); pack(); ko.addActionListener(this); setVisible(true); for (int i=0; i < t; i++) { ko.setLabel("" + (t-i)); try{ Thread.sleep(1000); } catch(InterruptedException o){} } }
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).
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).
You could use the following for the countdown and then start it like so after you have the UI all set up
Java Syntax (Toggle Plain Text)
class ButtonCountdown implements ActionListener { int count=0; JButton button = null; public ButtonCountdown(final JButton button, int count){ this.count = count; this.button = button; } public void actionPerformed(ActionEvent e) { if (count>0){ button.setText(String.valueOf(--count)); } else { button.setText("Done!"); } } }
Java Syntax (Toggle Plain Text)
Timer koTimer = new Timer(1000, new ButtonCountdown(ko, 5)); koTimer.start();
![]() |
Similar Threads
- Form data & Back button (PHP)
- glut (C)
- "click Counter" help Please (Visual Basic 4 / 5 / 6)
- counter.exe repair: HJT log review request (Viruses, Spyware and other Nasties)
- c:\counter.cab:\counter.exe (Viruses, Spyware and other Nasties)
- c:\counter.cab:\counter.exe (Viruses, Spyware and other Nasties)
- Random Restarts (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: Applet and i/o
- Next Thread: password Authentication without database
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows working






