Force a Java Tooltip to Appear

后端 未结 5 1014
慢半拍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条回答
  •  广开言路
    2021-01-07 22:58

    The only way (besides creating your own Tooltip window) I've found is to emmulate the CTRL+F1 keystroke on focus:

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

    Unfortunately, the tooltip will disappear as soon as you move your mouse (outside of the component) or after a delay (see ToolTipManager.setDismissDelay).

提交回复
热议问题