• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

JButton mousePressed - Show tool tip text  RSS feed

 
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
 
Author
Posts: 965
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
Posts: 965
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
 
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!