强制Java工具提示出现

Jos*_*eph 15 java tooltip

给定JTextField(或任何相关JComponent的),如何在没有来自用户的任何直接输入事件的情况下强制显示该组件的指定工具提示?换句话说,为什么没有JComponent.setTooltipVisible(boolean)

cam*_*ckr 5

您需要调用默认操作来显示工具提示。例如,要在组件获得焦点时显示工具提示,您可以将以下 FocusListener 添加到组件中:

FocusAdapter focusAdapter = new FocusAdapter()
{
    public void focusGained(FocusEvent e)
    {
        JComponent component = (JComponent)e.getSource();
        Action toolTipAction = component.getActionMap().get("postTip");

        if (toolTipAction != null)
        {
            ActionEvent postTip = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
            toolTipAction.actionPerformed( postTip );
        }

    }
};
Run Code Online (Sandbox Code Playgroud)

编辑:

上面的代码似乎不再起作用了。另一种方法是将 MouseEvent 分派给组件:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PostTipSSCCE extends JPanel
{
    public PostTipSSCCE()
    {
        FocusAdapter fa = new FocusAdapter()
        {
            public void focusGained(FocusEvent e)
            {
                JComponent component = (JComponent)e.getSource();

                MouseEvent phantom = new MouseEvent(
                    component,
                    MouseEvent.MOUSE_MOVED,
                    System.currentTimeMillis(),
                    0,
                    10,
                    10,
                    0,
                    false);

                ToolTipManager.sharedInstance().mouseMoved(phantom);
            }
        };

        JButton button = new JButton("Button");
        button.setToolTipText("button tool tip");
        button.addFocusListener( fa );
        add( button );

        JTextField textField = new JTextField(10);
        textField.setToolTipText("text field tool tip");
        textField.addFocusListener( fa );
        add( textField );

        JCheckBox checkBox =  new JCheckBox("CheckBox");
        checkBox.setToolTipText("checkbox tool tip");
        checkBox.addFocusListener( fa );
        add( checkBox );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("PostTipSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane(new PostTipSSCCE()) );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法会导致在显示工具提示之前稍微延迟,因为它模拟了鼠标进入组件。要立即显示工具提示,您可以使用 pstanton 的解决方案。


pst*_*ton 5

唯一的方法(除了创建自己的工具提示窗口)我发现是在焦点上设置CTRL + F1键击:

new FocusAdapter()
{
    @Override
    public void focusGained(FocusEvent e)
    {
        try
        {
            KeyEvent ke = new KeyEvent(e.getComponent(), KeyEvent.KEY_PRESSED,
                    System.currentTimeMillis(), InputEvent.CTRL_MASK,
                    KeyEvent.VK_F1, KeyEvent.CHAR_UNDEFINED);
            e.getComponent().dispatchEvent(ke);
        }
        catch (Throwable e1)
        {e1.printStackTrace();}
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,只要您移动鼠标(在组件外部)或延迟后(见ToolTipManager.setDismissDelay),工具提示就会消失.