Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for setToolTipText consuming mouse events?

This seems to be a verified problem with SWING

http://forums.sun.com/thread.jspa?threadID=385730

I'm currently trying to set the tooltip text of a tab in a JTabbedPane but when I do I can't actually select the tab anymore because the tooltip added a mouse listener that is consuming the events.

Does anyone know of a workaround that allows me to keep my tooltips AND my mouseevents? Thank you.

As per request here is my SSCCE

package jtabbedbug;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class JTabBug{

    public static void main(String[] args) {

      JTabbedPane jTabbedPane = new JTabbedPane();
      jTabbedPane.addTab("Red", new JLabel("Roses"));
      jTabbedPane.addTab("Blue", new JLabel("Skies"));
      jTabbedPane.addTab("Green", new JLabel("Grass"));

      for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
        JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i));
        tabComponent.setToolTipText("Tip: " + tabComponent.getText());
        jTabbedPane.setTabComponentAt(i, tabComponent);
      }

      JFrame jFrame = new JFrame("Testing");
      jFrame.add(jTabbedPane);
      jFrame.setSize(400, 500);
      jFrame.setVisible(true);
      jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
like image 857
Sandro Avatar asked Jul 07 '10 19:07

Sandro


People also ask

How do I see mouse events in the TestStand?

That is, the handlers are called in the order mousedown → mouseup → click. Click the button below and you’ll see the events. Try double-click too. On the teststand below all mouse events are logged, and if there is more than a 1 second delay between them they are separated by a horizontal ruler.

How do you trigger mouse events?

Mouse pointer comes over/out from an element. Every mouse move over an element triggers that event. Triggers after mousedown and then mouseup over the same element if the left mouse button was used. Triggers after two clicks on the same element within a short timeframe. Rarely used nowadays. Triggers when the right mouse button is pressed.

Is it possible to get the exact mouse button from a click?

Mouse button Click-related events always have the button property, which allows to get the exact mouse button. We usually don’t use it for click and contextmenu events, because the former happens only on left-click, and the latter – only on right-click.

How to prevent mouseDown in the chapter selection?

If one presses the left mouse button and, without releasing it, moves the mouse, that also makes the selection, often unwanted. There are multiple ways to prevent the selection, that you can read in the chapter Selection and Range. In this particular case the most reasonable way is to prevent the browser action on mousedown.


2 Answers

Here's one workaround:

import javax.swing.*;
import javax.swing.plaf.*;
import java.awt.event.*;

public class JTabBug{

    public static void main(String[] args) {

      JTabbedPane jTabbedPane = new JTabbedPane()
      {
        @Override
        public String getToolTipText(MouseEvent e)
        {
            int index = ((TabbedPaneUI)ui).tabForCoordinate(this, e.getX(), e.getY());

            if (index != -1)
            {
                JComponent component = (JComponent)getTabComponentAt(index);
                return component.getToolTipText();
            }

            return super.getToolTipText(e);
        }
      };
      ToolTipManager.sharedInstance().registerComponent(jTabbedPane);
      jTabbedPane.addTab("Red", new JLabel("Roses"));
      jTabbedPane.addTab("Blue", new JLabel("Skies"));
      jTabbedPane.addTab("Green", new JLabel("Grass"));

      for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
        JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i));
        tabComponent.setToolTipText("Tip: " + tabComponent.getText());
        ToolTipManager.sharedInstance().unregisterComponent(tabComponent);
        jTabbedPane.setTabComponentAt(i, tabComponent);
      }

      JFrame jFrame = new JFrame("Testing");
      jFrame.add(jTabbedPane);
      jFrame.setSize(400, 500);
      jFrame.setVisible(true);
      jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
like image 67
camickr Avatar answered Oct 26 '22 20:10

camickr


Seems to be working without any issues (under Java 6). Here is my code:

package com.twist.ui.widgets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {


        SwingUtilities.invokeLater( new Runnable() {

            @Override
            public void run() {

                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JTabbedPane tabs = new JTabbedPane();

                String s;
                for( int i=0; i<3; i++ ) {
                    s = "Tab " + (i+1);
                    tabs.add( new JPanel(), s );
                    tabs.setToolTipTextAt(i, "Tooltip for " + s);
                }
                f.setContentPane(tabs);


                f.setSize( 400,300);
                f.setLocationRelativeTo(null);
                f.setVisible(true);


            }
        });

    }


}
like image 20
Eugene Ryzhikov Avatar answered Oct 26 '22 22:10

Eugene Ryzhikov