Force a Java Tooltip to Appear

后端 未结 5 1013
慢半拍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 23:12

    You can access the ToolTipManager, set the initial delay to 0 and send it an event. Don't forget to restore the values afterwards.

    private void displayToolTip()
    {
        final ToolTipManager ttm = ToolTipManager.sharedInstance();
        final MouseEvent event = new MouseEvent(this, 0, 0, 0,
                                                0, 0, // X-Y of the mouse for the tool tip
                                                0, false);
        final int oldDelay = ttm.getInitialDelay();
        final String oldText = textPane.getToolTipText(event);
        textPane.setToolTipText("google!");
        ttm.setInitialDelay(0);
        ttm.setDismissDelay(1000);
        ttm.mouseMoved(event);
    
        new Timer().schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                ttm.setInitialDelay(oldDelay);
                textPane.setToolTipText(oldText);
            }
        }, ttm.getDismissDelay());
    }
    

提交回复
热议问题