Reputation: 9579
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);
Upvotes: 1
Views: 3730
Reputation: 109813
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
Upvotes: 2
Reputation: 9307
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.
Upvotes: 2