简体   繁体   English

如何在鼠标单击时显示工具提示

[英]How to show a tooltip on a mouse click

I have a JTreeTable and have successfully implemented a MouseMotionListener to show a tooltip whenever the mouse is over one of the cells. 我有一个JTreeTable并成功实现了MouseMotionListener ,只要鼠标位于其中一个单元格上,就会显示工具提示。 However when clicking on the cell the tooltip does not show up. 但是,单击单元格时,工具提示不会显示。 I've tried several things like setting the text on the mouseClicked and mouseReleased events but that doesn't work. 我尝试过几样的事情,比如在mouseClickedmouseReleased事件上设置文本,但这不起作用。 I found this code - 我发现这个代码 -

Action toolTipAction = treeTable.getActionMap().get("postTip");

if(toolTipAction != null){

   ActionEvent postTip = new ActionEvent(treeTable,ActionEvent.ACTION_PERFORMED, "");
   toolTipAction.actionPerformed(postTip);    
}

to use in the mouseReleased method, which does make the tooltip popup , but it's then in the wrong position. mouseReleased方法中使用,它确实使tooltip popup ,但它在错误的位置。 So next i tried overriding the getTooltipLocation method on the JTreeTable , and this works fine for mouseMoved events but doesn't get called with the above method. 所以接下来我尝试覆盖JTreeTable上的getTooltipLocation方法,这对于mouseMoved事件很有效,但是没有用上面的方法调用。 Can anyone shed some light on how to do this? 任何人都可以阐明如何做到这一点?

Thanks Andy 谢谢安迪

You can use the following approach to show the tooltip (there will be a slight delay). 您可以使用以下方法显示工具提示(稍有延迟)。 Then you can override the getToolTipLocation() method since a MouseEvent will now be generated: 然后您可以覆盖getToolTipLocation()方法,因为现在将生成MouseEvent:

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();
            }
        });
    }
}

org.apache.jorphan.gui.JTreeTable extends javax.swing.JComponent javax.swing.JComponent#setToopTipText() doesn't work? org.apache.jorphan.gui.JTreeTable扩展javax.swing.JComponent javax.swing.JComponent #setToopTipText()不起作用? I do realize that you want to use Action but for tooltips? 我确实意识到你想使用Action而不是工具提示? I would use Action when multiple UI actions would need to share it. 当多个UI操作需要共享时,我会使用Action。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM