How to show tooltip on mouse click

I have a JTreeTable and successfully implemented MouseMotionListener to show a tooltip whenever the mouse is over one of the cells. However, when you click on a cell, a tooltip does not appear. I tried several things, such as setting text in mouseClicked and mouseReleased , but this does not work. I found this code -

 Action toolTipAction = treeTable.getActionMap().get("postTip"); if(toolTipAction != null){ ActionEvent postTip = new ActionEvent(treeTable,ActionEvent.ACTION_PERFORMED, ""); toolTipAction.actionPerformed(postTip); } 

for use in the mouseReleased method, which makes the tooltip popup , but it is in the wrong position. So, I tried to override the getTooltipLocation method on JTreeTable , and this works great for mouseMoved events, but it is not called using the above method. Can anyone shed some light on how to do this?

Thanks Andy

+7
source share
2 answers

You can use the following approach to display a tooltip (there will be a slight delay). You can then override the getToolTipLocation () method since MouseEvent will now be created:

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ToolTipOnRelease extends JPanel { public ToolTipOnRelease() { JLabel label = new JLabel( "First Name:" ); add( label ); JTextField textField = new JTextField(15); add( textField ); MouseListener ml = new MouseAdapter() { public void mouseReleased(MouseEvent e) { JComponent component = (JComponent)e.getSource(); component.setToolTipText("Mouse released on: " + component.getClass().toString()); MouseEvent phantom = new MouseEvent( component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 0, 0, 0, false); ToolTipManager.sharedInstance().mouseMoved(phantom); } }; label.addMouseListener( ml ); textField.addMouseListener( ml ); } private static void createAndShowUI() { JFrame frame = new JFrame("ToolTipOnRelease"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( new ToolTipOnRelease() ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } 
+11
source

org.apache.jorphan.gui.JTreeTable extends javax.swing.JComponent javax.swing.JComponent # setToopTipText () not working? I really understand that you want to use Action, but for tooltips? I would use Action when you would need to use several user interface actions.

0
source

All Articles