Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread timer and a locked swing button!!!! 1

Status
Not open for further replies.

xscape

Programmer
Apr 18, 2001
144
GB
Hi,

I've written a timer that is pretty simple and uses a thread to increment seconds, then sleep(1000), to wait 1 seconds then loop again and increment a seconds again. This thread just runs in a while loop. I use a System.out.println to print out the result each time the loop runs through a cycle.

I've written a GUI using Swing for it so there's a start, pause and reset button which all works fine. So I now have a Timer class and a Gui class. However the time is displayed on the system prompt screen, not in the GUI. I wanted to display it in a text box so I can see it count upwards in the GUI.

I tried a while loop that runs in the start buttons action listener method, and it simply takes the result from a method in the timer class over and over and displays it so it does work! HOWEVER, while the loop is running the Start button remains locked down, and none of the other buttons can be pressed, and the window won't close, so I have to terminate it from the system prompt!

Is there any way that I can display the result in a text box so it's only updated when the thread increments a second? I'm sure there must be a simple way around this but I haven't figured it out yet!
 
In the text box there will be a variable representing the box's text. All you have to do is set that variable to your incremented time and the GUI will be automaticaly updated for you. For example, say you creat a JPanel and add a JLabel to it. The JLabel will be the text. Then you just have to call the method setText(String text) on the JLabel. As for the button sticking problem, how about some code? That would help us help you. MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
Thanks for your help. That was kind of what I was after, I just needed it to repaint the panel. The reason for the locked button was a while loop that was triggered in order to repaint the panel, and could never be stopped as it kept that button permentatly active. I've sorted it now, thanks very much for your input - have a star! :)
 
You've now got a star next to your name, sorry forgot to do that in my last post! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top