Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a Java Tooltip to Appear

Tags:

java

tooltip

Given a JTextField (or any JComponent for that matter), how would one go about forcing that component's designated tooltip to appear, without any direct input event from the user? In other words, why is there no JComponent.setTooltipVisible(boolean)?

like image 903
Joseph Avatar asked Jun 24 '11 20:06

Joseph


1 Answers

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).

like image 162
pstanton Avatar answered Oct 25 '22 03:10

pstanton