• Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton mousePressed - Show tool tip text

 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to show the JButton tool tip on the mousePressed event of the JButton Action Listener; the same way as if the user did a mouseOver.

Thanks for your help, Lisa
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came up with this and it seems to work.

public void actionPerformed(ActionEvent ae) {
if (!(ae.getSource() instanceof JComponent)) return;
JComponent source = (JComponent)ae.getSource();
Action postTip = source.getActionMap().get("postTip");
if (postTip == null) return;
postTip.actionPerformed(ae); // show the tooltip
}
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, I gave you an actionPerformed() but it seems you really want a mousePressed(). The code would be the same except you must instantiate an ActionEvent to be the argument for postTip.actionPerformed().
 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! That worked great and it's really straight forward.

Thanks for your help, Lisa
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here is a class that extends JButton. The constructor takes an image and a tooltiptext. You can set the tooltip wherever you want

// Mathias
 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!