aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Mimicking a JButton Click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Mimicking a JButton Click" Watch "Mimicking a JButton Click" New topic
Author

Mimicking a JButton Click

Tim Alvord
Greenhorn

Joined: Jan 24, 2013
Posts: 23
Hi,
I have calss JPButton that extends JButton. It basically is a graphical button that shifts 1 pixel to the right and down when mousePressed(), and shifts back when mouseReleased(). Both of those functions are Overriden obviously. The question I have is that I'm trying to have a keypress mimick the Mouse action. I attempted to do this by Overriding doClick(). Doesn't seem to work. Is there a preferred way to do this?




Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you move all of the code from inside mousePressed/released/whatever to separate methods,
called from mousePressed/released/whatever. Those methods can now be called from your
keyListener methods.
Tim Alvord
Greenhorn

Joined: Jan 24, 2013
Posts: 23
Michael,
Did that. Same result. It executes the code, but the button doesn't move when called from doClick()...


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Thread.sleep(500L);

will block any painting/listening - use a Swing.Timer for delay purposes
Tim Alvord
Greenhorn

Joined: Jan 24, 2013
Posts: 23
Michael,
Got it to work by doing the following:

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mimicking a JButton Click