views:

1268

answers:

2

I'm trying to set tooltips on a JEditorPane. The problem is that the method which I use to determine what tooltip text to show is fairly CPU intensive - and so I would like to only show it after the mouse has stopped for a short amount of time - say 1 second.

I know I can use :- ToolTipManager.sharedInstance().setInitialDelay() however this will set the delay time for tooltips on all swing components at once and I don't want this.

Does anyone have an idea how you could do this?

Thanks

+3  A: 

Well, I would recommend doing the CPU intensive task on another thread so it doesn't interrupt normal GUI tasks.

That would be a better solution. (instead of trying to circumvent the problem)

*Edit* You could possibly calculate the tootips for every word in the JEditorPane and store them in a Map. Then all you would have to do is access the tootip out of the Map if it changes.

Ideally people won't be moving the mouse and typing at the same time. So, you can calculate the tootlips when the text changes, and just pull them from the Map on mouseMoved().

jjnguy
Your edit is a very good suggestion. I'm not sure if it will work because it is actually a debugger I am working on and so the values that are put in the map are subject to change - it's a great idea though. I think your suggestion of computing the value in a thread is good too.
Scottm
Yes I agree - some form of caching the answer as the mouse moves and retrieving it when the user stops could solve my problem. I will give it a try tomorrow - I'll set your answer to accepted. Thanks for your help.
Scottm
+1  A: 

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

tulskiy
In my case, if you hover above an icon at the start of the status bar, a tooltip pops up with the past 'n' messages. As long as you keep the mouse within the tooltip bounds it stays. But I wanted to control all timing aspects without changing the global tooltip timings.In my opinion, your solution seems like a much better alternative than the accepted answer, but hey its only my two cents? Thanks for this solution Piligrim, I'll be trying it very shortly!
Jeach