Alex
Alex

Reputation: 660

Detect a toolTip launch java

Hey, is there any way to detect the launch of a tool tip on a swing component in Java? Cant even think where to start on this one. cheers

Upvotes: 2

Views: 1303

Answers (2)

Tomas Narros
Tomas Narros

Reputation: 13468

You could overwrite the createTooltip method on your specific component, adding the JTooltip element returned a ComponentListener.

As an example:

   final ComponentListener customTooltipListener=new ComponentListener() {

        @Override
        public void componentHidden(ComponentEvent e) {
            // whatever you need on this event
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            // whatever you need on this event
        }

        @Override
        public void componentResized(ComponentEvent e) {
            // whatever you need on this event

        }

        @Override
        public void componentShown(ComponentEvent e) {
            // whatever you need on this event

        }
    };          

    JButton button=new JButton("Command") {

        @Override
        public JToolTip createToolTip() {
            //keep default behaviour 
            JToolTip toReturn=super.createToolTip();
            toReturn.addComponentListener(customTooltipListener);
            return toReturn;
        }

    };

That should be enough.

Upvotes: 1

AlexR
AlexR

Reputation: 115328

My short investigation showed the following.

Class responsible on the tooltip appearance is TootipManager. All components use its shared instance, so this manager is singleton. You cannot intercept creating this manager and it does not throw events that you can catch. But when manger decides to show the tooltip it calls getToolTipText() from the component. So, if you wish to know that manager called it subclass your component (JButton, JList etc), override method getToolTipText() and use new Trowable().getStackTrace() to identified that you are called from TooltipManager.show()

Good luck.

Upvotes: 1

Related Questions