• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

button problem

 
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use a Timer
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks i will try out...
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am pretty sure there is something about Timers in the Java Tutorials. Yes, try here.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys for the help......

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

problem code :






help me....
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what goes wrong? We can’t help without more details.
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Can you explain me why have you made String originalText as final....

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

 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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






 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic