Jump to content

Showing a JToolTip

- - - - -

  • Please log in to reply
7 replies to this topic

#1
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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.
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
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Hm that was surprisingly hard

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
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Wow, that is just a bit trickier than I thought.

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
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
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.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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 :)

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
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.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Didn't know that was possible. After looking it up, it appeirs the dispatchEvent() method of JComponent can do that...

#8
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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!
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