Force a Java Tooltip to Appear

后端 未结 5 1035
慢半拍i
慢半拍i 2021-01-07 22:51

Given a JTextField (or any JComponent for that matter), how would one go about forcing that component\'s designated tooltip to appear, without any

5条回答
  •  萌比男神i
    2021-01-07 23:10

    You need to invoke the default Action to show the tooltip. For example to show a tooltip when a component gains focus you can add the following FocusListener to the component:

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

    Edit:

    The above code doesn't seem to work anymore. Another approach is dispatch a MouseEvent to the component:

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

    This approach will result in a slight delay before the tooltip is displayed as it simulated the mouse entering the component. For immediate display of the tooltip you can use pstanton's solution.

提交回复
热议问题