Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How can I reset the text of button every second?

Reply
Thread Tools

How can I reset the text of button every second?

 
 
jtl.zheng
Guest
Posts: n/a
 
      09-01-2006
I want to set the button's text every second
as: 1 -> 2 -> 3 -> 4 .........
my code is:
------------------------------------
public class Ooop {
public static void doIt() {
JFrame jFrame = new JFrame();
JButton jButton = new JButton("xxx");
jFrame.add(jButton);
jFrame.pack();
jFrame.setVisible(true);

int i=0;
while(true){
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
}
jButton.setText(String.valueOf(i++));
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
doIt();
}
});
}
}
-------------------------------

I use Thread.sleep() to do it
but it can not work....
It show nothing all the time
just a blank frame, not button at all
why? is it about the Thread the UI using?
how can I reset the text fo button every second?

Thank you very much in advance.

 
Reply With Quote
 
 
 
 
Ike
Guest
Posts: n/a
 
      09-01-2006
Why not use a Timer ?


 
Reply With Quote
 
 
 
 
jtl.zheng
Guest
Posts: n/a
 
      09-01-2006
Thank you very much
it work out now
:

timer = new java.util.Timer(true);
timer.schedule(
new java.util.TimerTask() {
public void run() {
jButton.setText(String.valueOf(i++));
if (i > 5) {
timer.cancel();
}
}
}, 0, 1000);

 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      09-01-2006

"jtl.zheng" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I want to set the button's text every second
> as: 1 -> 2 -> 3 -> 4 .........
> my code is:
> ------------------------------------
> public class Ooop {
> public static void doIt() {
> JFrame jFrame = new JFrame();
> JButton jButton = new JButton("xxx");
> jFrame.add(jButton);
> jFrame.pack();
> jFrame.setVisible(true);
>
> int i=0;
> while(true){
> try {
> Thread.sleep(1000);
> }
> catch (InterruptedException ex) {
> }
> jButton.setText(String.valueOf(i++));
> }
> }
> public static void main(String[] args) {
> javax.swing.SwingUtilities.invokeLater(new Runnable() {
> public void run() {
> doIt();
> }
> });
> }
> }
> -------------------------------
>
> I use Thread.sleep() to do it
> but it can not work....
> It show nothing all the time
> just a blank frame, not button at all
> why? is it about the Thread the UI using?


Yes, exactly. Your counter has taken over the event dispatch thread which
now cannot update the screen. The Timer class will run the counting and
waiting in a separate thread so that only the actual button update takes
place in the EDT (which is necessary).

Matt Humphrey http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      09-01-2006
jtl.zheng wrote:
> Thank you very much
> it work out now
> :
>
> timer = new java.util.Timer(true);


It might work, but it's not good. Threads and Swing don't mix. It might
work now, but then you might later (perhaps on a custom machine) can a
problem which does not appear to be related to this code.

java.util.Timer will create its own thread and execute code there. You
need to be on the normal "AWT Event Dispatch Thread" (EDT). So use
javax.swing.Timer instead, which calls java.awt.EventQueue.invokeLater
under the covers.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
jtl.zheng
Guest
Posts: n/a
 
      09-01-2006
Thank you very much for your help

I know a little now
what I guess is:
all the swing components have only one single thread to repaint
themselves
and it repaint themselves all the time
so if I call the Thread.sleep(500); inside this swing thread as I did
it stop the thread and also stop the repainting,
so it show nothing but a blank frame(not buttons)
is it right?

so if I want to reset the text of button every second
I must call another thread outside this swing thread
or use javax.swing.Timer as you said
is it right?

Thank you very much in advance

 
Reply With Quote
 
Rogan Dawes
Guest
Posts: n/a
 
      09-01-2006
jtl.zheng wrote:
> Thank you very much for your help
>
> I know a little now
> what I guess is:
> all the swing components have only one single thread to repaint
> themselves
> and it repaint themselves all the time
> so if I call the Thread.sleep(500); inside this swing thread as I did
> it stop the thread and also stop the repainting,
> so it show nothing but a blank frame(not buttons)
> is it right?
>
> so if I want to reset the text of button every second
> I must call another thread outside this swing thread
> or use javax.swing.Timer as you said
> is it right?
>
> Thank you very much in advance
>


No, in fact you want to make sure that the code that updates the UI is
actually executing on the EDT (Event Dispatching Thread).

So, it makes a big difference whether you use java.util.Timer, or
javax.swing.Timer

java.util.Timer will execute your task on an internal thread created by
the Timer, while javax.swing.Timer will make sure that your task is
executed on the EDT.

Regards,

Rogan
 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      09-01-2006

"Rogan Dawes" <(E-Mail Removed)> wrote in message
news:ed9mkr$ada$(E-Mail Removed)...
> jtl.zheng wrote:
>> Thank you very much for your help
>>
>> I know a little now
>> what I guess is:
>> all the swing components have only one single thread to repaint
>> themselves
>> and it repaint themselves all the time
>> so if I call the Thread.sleep(500); inside this swing thread as I did
>> it stop the thread and also stop the repainting,
>> so it show nothing but a blank frame(not buttons)
>> is it right?
>>
>> so if I want to reset the text of button every second
>> I must call another thread outside this swing thread
>> or use javax.swing.Timer as you said
>> is it right?
>>
>> Thank you very much in advance
>>

>
> No, in fact you want to make sure that the code that updates the UI is
> actually executing on the EDT (Event Dispatching Thread).
>
> So, it makes a big difference whether you use java.util.Timer, or
> javax.swing.Timer
>
> java.util.Timer will execute your task on an internal thread created by
> the Timer, while javax.swing.Timer will make sure that your task is
> executed on the EDT.


Rogan is quite right, but to continue to underscore the key point here (at
the risk of beating a dead horse) the *waiting* must *not* happen in the
EDT. The *updating* must happen on the EDT. javax.swing.Timer does both
for you.

The code you're looking for is something like this:

int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton.setText(String.valueOf(i++));
}
};

Timer aTimer = new Timer(delay, taskPerformer);
aTimer.start ();

Matt Humphrey (E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
jtl.zheng
Guest
Posts: n/a
 
      09-02-2006
> No, in fact you want to make sure that the code that updates the UI is
> actually executing on the EDT (Event Dispatching Thread).


So there are only one EDT thread at all
and any event handling or painting are all executed in this single EDT
if I call the Thread.sleep() inside EDT ,it also stop the painting,so
it just display a blank frame
and the swing.Timer also invoke the codes inside this EDT,so it's safe
to change the components
is it right?

 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      09-02-2006

"jtl.zheng" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
>> No, in fact you want to make sure that the code that updates the UI is
>> actually executing on the EDT (Event Dispatching Thread).

>
> So there are only one EDT thread at all
> and any event handling or painting are all executed in this single EDT
> if I call the Thread.sleep() inside EDT ,it also stop the painting,so
> it just display a blank frame
> and the swing.Timer also invoke the codes inside this EDT,so it's safe
> to change the components
> is it right?


I think you're getting the idea now. The swing Timer has a separate thread
on which it does the waiting, but when it wakes up, it uses the
SwingUtilities.invokeLater to send the action to the EDT thread for
execution.

Matt Humphrey (E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
You will not be able to reset the access - can't reset. jc MCAD 0 12-09-2007 08:58 PM
Explorer.exe error report every time access every type of video me =?Utf-8?B?SXJ3YW5zeWFo?= ASP .Net 4 10-30-2007 07:49 AM
HELP! dropdown control is being reset to a value on every postback! Mad Scientist Jr ASP .Net 1 06-25-2004 02:12 PM
HTML Reset Button Doesn't work after postback with Submit button Chris Lane ASP .Net 4 11-17-2003 11:52 PM



Advertisments