Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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
Ranch Hand
Posts: 964
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: 964
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!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!