*
The moose likes Swing / AWT / SWT and the fly likes button problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "button problem" Watch "button problem" New topic
Author

button problem

abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

hi, help me with this code....



Output:

before clicking:



after clicking:




Problem:

after clicking i want to rest the button setText() to click me (again) after 2 seconds...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
use a Timer
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2168
    
  47
More explicitly use a javax.swing.Timer.

The reason you are better off using the swing Timer as opposed to the java.util.Timer is the swing timer runs the action event listeners on the Event Dispatch Thread so you can directly manipulate swing components.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

thanks i will try out...
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

yes i used the Timer and the button got freeze for the required time but the problem how do i set

the old text (i.e "click me") again.


this is my modified code:

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2168
    
  47
I thought you wanted to wait 2 seconds and not 50 milliseconds!

You need to pass an ActionListener into the Timer's constructor.
You are passing 'this' which means when the timer completes it will call this objects actionPerformed method which creates and starts a new timer again and so on and so on.
BTW This is a good place to use an anonymous inner class ie

By default Timers repeat every n milliseconds so before starting it you should call
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37933
    
  22
Tony Docherty wrote: . . .This is a good place to use an anonymous inner class . . .
As a general rule, I like anonymous classes, provided you only use them once each. I do not like addActionListener(this), because it often denotes failure to use proper object design.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2168
    
  47
Campbell Ritchie wrote:I do not like addActionListener(this), because it often denotes failure to use proper object design.

Agreed, especially when the actionPerformed method has a list of if-else statements which take some action depending on which component triggered the event.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Tony Docherty wrote:I thought you wanted to wait 2 seconds and not 50 milliseconds!

You need to pass an ActionListener into the Timer's constructor.
You are passing 'this' which means when the timer completes it will call this objects actionPerformed method which creates and starts a new timer again and so on and so on.
BTW This is a good place to use an anonymous inner class ie

By default Timers repeat every n milliseconds so before starting it you should call



thanks... my code is working well now....

but as i am a beginner i am having some confusion with the Timer.....

can you share some good and easy article for it to learn....


thanks...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37933
    
  22
I am pretty sure there is something about Timers in the Java Tutorials. Yes, try here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37933
    
  22
Tony Docherty wrote: . . . when the actionPerformed method has a list of if-else statements . . .
And if that isn’t non‑object‑oriented design, I don’t know what is!
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

thank you guys for the help......

i have got a new problem with the above code...

problem code :






help me....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37933
    
  22
And what goes wrong? We can’t help without more details.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Campbell Ritchie wrote:And what goes wrong? We can’t help without more details.



i have written the problem as comment in my code


(i.e i want to get the original b1.setText("CLICK ME") which i have set in go() in the Timer class actionPerformed())...

thank you
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it seems to work OK as is (by setting the same string), but if you want the original

in class's actionPerformed add this line
final String originalText = b1.getText();//<----place above line below
b1.setText("BUTTON CLICKED");

now in the timer's actionPerformed, change
b1.setText("CLICK ME (newly set)");
to
b1.setText(originalText);
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Thanks Michael Dunn

Now my code is running perfect, i really love this forum and you guys.....


Can you explain me why have you made String originalText as final....


thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Can you explain me why have you made String originalText as final....

remove the word 'final', recompile, see what happens.

abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Michael Dunn wrote:> Can you explain me why have you made String originalText as final....

remove the word 'final', recompile, see what happens.




yes i removed and tried it gives the follwoing error message:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - local variable originalText is accessed from within inner class; needs to be declared final
at EventDemo$1.actionPerformed(EventDemo.java:41)
at javax.swing.Timer.fireActionPerformed(Timer.java:312)
at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)



"local variable originalText is accessed from within inner class; needs to be declared final" please explain me this line...


thanks






Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

abrar alvi wrote:"local variable originalText is accessed from within inner class; needs to be declared final" please explain me this line...

Read all about it here.
 
 
subject: button problem
 
Similar Threads
change back text of button with help of Thread.sleep()
Customizing JToolTip
Where have I gone wrong?
Calculator Problem
Paint method is covering other layouts