Set the Tooltip Delay Time for a Particular Component in Java Swing

前端 未结 4 1180
不思量自难忘°
不思量自难忘° 2020-12-29 21:39

I\'m trying to set tooltips on a JEditorPane. The method which I use to determine what tooltip text to show is fairly CPU intensive - and so I would like to onl

4条回答
  •  庸人自扰
    2020-12-29 22:04

    You can show the popup yourself. Listen for mouseMoved() events, start/stop the timer and then show popup with the following code:

    First you need PopupFactory, Popup, and ToolTip:

    private PopupFactory popupFactory = PopupFactory.getSharedInstance();
    private Popup popup;
    private JToolTip toolTip = jEditorPane.createToolTip();
    

    then, to show or hide the toolTip:

    private void showToolTip(MouseEvent e) {
        toolTip.setTipText(...);
        int x = e.getXOnScreen();
        int y = e.getYOnScreen();
        popup = popupFactory.getPopup(jEditorPane, toolTip, x, y);
        popup.show();
    }
    
    private void hideToolTip() {
        if (popup != null)
            popup.hide();
    }
    

    This will give you adjustable delay and a lot of troubles :)

提交回复
热议问题