• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mimicking a JButton Click

 
Tim Alvord
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
Did that. Same result. It executes the code, but the button doesn't move when called from doClick()...


 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.sleep(500L);

will block any painting/listening - use a Swing.Timer for delay purposes
 
Tim Alvord
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
Got it to work by doing the following:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic