Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Rob Spoor
Junilu Lacar
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Piet Souris
Carey Brown
Bartenders:
Forum:
Swing / AWT / SWT
JButton mousePressed - Show tool tip text
Lisa Zapson
Ranch Hand
Posts: 50
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 986
3
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
}
bitguru blog
Brian Cole
Author
Posts: 986
3
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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().
bitguru blog
Lisa Zapson
Ranch Hand
Posts: 50
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Excellent! That worked great and it's really straight forward.
Thanks for your help, Lisa
Mathias Nilsson
Ranch Hand
Posts: 367
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import javax.swing.JButton; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.ActionListener; import javax.swing.Action; import java.awt.Insets; import javax.swing.border.Border; import javax.swing.border.SoftBevelBorder; import javax.swing.border.BevelBorder; import javax.swing.border.EmptyBorder; import javax.swing.Icon; import javax.swing.ImageIcon; public class SmallButton extends JButton implements MouseListener { protected Border m_raised = new SoftBevelBorder(BevelBorder.RAISED); protected Border m_lowered = new SoftBevelBorder(BevelBorder.LOWERED); protected Border m_inactive = new EmptyBorder(0,0, 0, 0); protected Border m_border = m_inactive; protected Insets m_ins = new Insets(0,0,0,0); public SmallButton( ImageIcon icon, String tip) { super( icon ); setBorder(m_inactive); setMargin(m_ins); setToolTipText(tip); setRequestFocusEnabled(false); addMouseListener(this); } public float getAlignmentY() { return 0.5f; } // Overridden for 1.4 bug fix public Border getBorder() { return m_border; } // Overridden for 1.4 bug fix public Insets getInsets() { return m_ins; } public void mousePressed(MouseEvent e) { if( this.isEnabled() ){ m_border = m_lowered; setBorder(m_lowered); } } public void mouseReleased(MouseEvent e) { m_border = m_inactive; setBorder(m_inactive); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) { if( this.isEnabled() ){ //m_border = m_raised; //setBorder(m_raised); } } public void mouseExited(MouseEvent e) { m_border = m_inactive; setBorder(m_inactive); } }
Here is a class that extends JButton. The constructor takes an image and a tooltiptext. You can set the tooltip wherever you want
// Mathias
SCJP1.4
Lisa Zapson
Ranch Hand
Posts: 50
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks!
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
creating tooltips?
JVM memory releasing after memory consuming task
Help on Tooltip
Tool Tip problem
tool tip
More...