How can one show a JToolTip programmatically, rather than wait for a user to hover over the associated control? I tried using object.createToolTip().setVisible(true), but that didn't seem to work.
7 replies to this topic
#1
Posted 30 March 2011 - 05:57 AM
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
|
|
|
#2
Posted 30 March 2011 - 08:35 AM
Hm that was surprisingly hard
I would just override the paint method of the frame and paint a square with text in it myself ^^. As if you move the frame, the tooltip remains at its position, which looks rediculous.
public class TooltipTest extends JFrame {
private JToolTip tooltip;
private JLabel label;
private JButton button;
public TooltipTest() {
super("tooltipFrame");
label = new JLabel("label");
tooltip = label.createToolTip();
tooltip.setTipText("Toooooltip");
add(label, BorderLayout.NORTH);
button = new JButton("Show tooltip");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
int x = label.getLocationOnScreen().x;
int y = label.getLocationOnScreen().y;
x+= label.getWidth()/2;
y+= label.getHeight();
final Popup tooltipContainer = popupFactory.getPopup(label, tooltip, x, y);
tooltipContainer.show();
(new Thread(new Runnable(){
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(TooltipTest.class.getName()).log(Level.SEVERE, null, ex);
}
tooltipContainer.hide();
}
})).start();
}
});
add(button, BorderLayout.SOUTH);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
TooltipTest test = new TooltipTest();
}
}
I would just override the paint method of the frame and paint a square with text in it myself ^^. As if you move the frame, the tooltip remains at its position, which looks rediculous.
#3
Posted 30 March 2011 - 12:42 PM
Wow, that is just a bit trickier than I thought.
Thanks for the help!
Thanks for the help!
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#4
Posted 30 March 2011 - 03:42 PM
I tried playing around with javax.swing.ToolTipManager but couldn't quite figure it out... headache now :(
I'm thinking you might be able to "fake" a mouse event to make the component think the mouse has entered the region.
I'm thinking you might be able to "fake" a mouse event to make the component think the mouse has entered the region.
#5
Posted 30 March 2011 - 11:16 PM
Hah, yes faking the mouse is possible too.
Maybe if you use the tooltipmanager to set the initialdelay (amount of time the mouse has to be still on the object to show the tooltip) very very low.
And the dismissDelay(amount of time before it disappeirs) very very high. And Then use the Robot class to put your mouse on the object, wait a few ms and then put the mouse back at its original position.
That would possibly work. I just don't like the idea of Java controlling my mouse :)
Maybe if you use the tooltipmanager to set the initialdelay (amount of time the mouse has to be still on the object to show the tooltip) very very low.
And the dismissDelay(amount of time before it disappeirs) very very high. And Then use the Robot class to put your mouse on the object, wait a few ms and then put the mouse back at its original position.
That would possibly work. I just don't like the idea of Java controlling my mouse :)
#6
Posted 31 March 2011 - 03:38 AM
Yes use part of that answer, but instead of actually moving the mouse with the Robot class, send a user-created action that gives coordinates to where the tooltip component is.
*EDIT*
Some quick googling and I came across this:
java - How to change the jtooltip timers for 1 component. - Stack Overflow
Read Camickr's post near the bottom. Something like this might work while also setting the delay to Integer.MAX_VALUE through ToolTipManager.
*EDIT*
Some quick googling and I came across this:
java - How to change the jtooltip timers for 1 component. - Stack Overflow
Read Camickr's post near the bottom. Something like this might work while also setting the delay to Integer.MAX_VALUE through ToolTipManager.
#7
Posted 31 March 2011 - 03:41 AM
Didn't know that was possible. After looking it up, it appeirs the dispatchEvent() method of JComponent can do that...
#8
Posted 31 March 2011 - 06:33 AM
I like all the discussion on this topic. It's intriguing.
As for my application, I think wim DC's original suggestion works best, even though it's a bit more extra work, since I'm trying to trigger the tool tip popup on a mouse click event. The application I'm building is designed for touch screen clients, and thus there is no such thing as a hover event. The popups are designed to give hints on a database input form, so it's not really an issue that the popup hangs in the same place when the user scrolls or moves the form, since the user probably won't be scrolling the form for at least 5 seconds after they read the tool tip, since they then have to fill in that field.
Thanks for all the input! This was a good one!
As for my application, I think wim DC's original suggestion works best, even though it's a bit more extra work, since I'm trying to trigger the tool tip popup on a mouse click event. The application I'm building is designed for touch screen clients, and thus there is no such thing as a hover event. The popups are designed to give hints on a database input form, so it's not really an issue that the popup hangs in the same place when the user scrolls or moves the form, since the user probably won't be scrolling the form for at least 5 seconds after they read the tool tip, since they then have to fill in that field.
Thanks for all the input! This was a good one!
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









