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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7353021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:37:33  来源:igfitidea点击:

How to show a tooltip on a mouse click

javaswingtooltipjtreetable

提问by user935339

I have a JTreeTableand have successfully implemented a MouseMotionListenerto show a tooltip whenever the mouse is over one of the cells. However when clicking on the cell the tooltip does not show up. I've tried several things like setting the text on the mouseClickedand mouseReleasedevents but that doesn't work. I found this code -

我有一个JTreeTable并且已经成功地实现了MouseMotionListener一个在鼠标悬停在其中一个单元格上时显示工具提示的方法。但是,当单击单元格时,工具提示不会显示。我尝试了几种方法,例如在mouseClickedmouseReleased事件上设置文本,但这不起作用。我找到了这个代码 -

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

if(toolTipAction != null){

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

to use in the mouseReleasedmethod, which does make the tooltip popup, but it's then in the wrong position. So next i tried overriding the getTooltipLocationmethod on the JTreeTable, and this works fine for mouseMovedevents but doesn't get called with the above method. Can anyone shed some light on how to do this?

mouseReleased方法中使用,这确实使tooltip popup,但它处于错误的位置。所以接下来我尝试覆盖 上的getTooltipLocation方法JTreeTable,这对mouseMoved事件很好用,但不能用上面的方法调用。谁能解释一下如何做到这一点?

Thanks Andy

谢谢安迪

回答by camickr

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

回答by Ustaman Sangat

org.apache.jorphan.gui.JTreeTable extends javax.swing.JComponent javax.swing.JComponent#setToopTipText() doesn't work? I do realize that you want to use Action but for tooltips? I would use Action when multiple UI actions would need to share it.

org.apache.jorphan.gui.JTreeTable 扩展 javax.swing.JComponent javax.swing.JComponent#setToopTipText() 不起作用?我确实意识到您想使用 Action 而不是工具提示?当多个 UI 操作需要共享它时,我会使用 Action。