1

I need to programically show tooltip for my button2 when it gets focus. (I press Tab as intially focus at button1)

            JButton button = new JButton("Button 1");

            JButton button2 = new JButton("Button 2");
            button2.setToolTipText("tooltip2");
            button2.addFocusListener(new FocusListener());

I refer the code by @camickr

private class FocusListener extends FocusAdapter {
 public void focusGained(FocusEvent e)
 {
    JComponent component = (JComponent)e.getSource();
    Action toolTipAction = component.getActionMap().get("postTip");

but toolTipAction is set null.

I have printed all the entries of ActionMap using this code

        ActionMap actionMap = component.getActionMap();
        Object[] actionMapKeys = actionMap.allKeys();

        for (int i = 0; i < actionMapKeys.length; i++) {
            Object key = actionMapKeys[i];
            System.out.println(key.toString() + " : " + actionMap.get(key).toString());
        }

This is what it gives me

pressed : javax.swing.plaf.basic.BasicButtonListener$Actions@49cf9f
released : javax.swing.plaf.basic.BasicButtonListener$Actions@1de0b5e

So how can I call this code if I got toolTipAction null?

ActionEvent postTip = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed(postTip);
Community
  • 1
  • 1
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101

2 Answers2

2

You could also try alternative approach of configuring the tooltip manager to immediately show the tooltip when mouse is entered.

javax.swing.ToolTipManager.sharedInstance().setInitialDelay(0)

If you want this to happen only for some components then you could change this value depending on the component that gets focus.

Tooltip is also shown when Ctrl+F1 is pressed. So may be you could simulate a Ctrl+F1 using java.awt.Robot on the button when you want to display tooltip.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
  • actuall I want to show tooltip on some network event, which has nothing to do with mouse. but I have started experiments with focus gained by Tab key – Nikolay Kuznetsov Nov 28 '12 at 06:41
2

actuall I want to show tooltip on some network event, which has nothing to do with mouse. but I have started experiments with focus gained by Tab key

  • use JWindow (undecorated JDialog) or JLabel instead of ToolTip, examples for JLabel by @Guillaume Polet and here

  • you can to lay this container to the Mouse Cursor possition or to stick to absolute coordinates, Point to the visible GUI

  • standard could be (can be annoying) see SystemTray and TrayIcon#displayMessage

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    @NikolayKuznetsov You can also take a look at [this answer](http://stackoverflow.com/questions/12822819/dynamically-update-tooltip-currently-displayed/12823177#12823177) where I automatically make a tooltip appear on a component although I don't have any actual MouseEvent (we dynamically create a fake one): `ToolTipManager.sharedInstance().mouseMoved( new MouseEvent(component, -1, System.currentTimeMillis(), 0, locationOnComponent.x, locationOnComponent.y, locationOnScreen.x, locationOnScreen.y, 0, false, 0));` – Guillaume Polet Nov 28 '12 at 09:34