消耗鼠标事件的setToolTipText的解决方法?

时间:2010-07-07 19:15:02

标签: java swing tooltip mouseevent

这似乎是SWING验证的问题

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

我正在尝试在JTabbedPane中设置标签的工具提示文本,但是当我这样做时,我实际上无法再选择标签,因为工具提示添加了一个消耗事件的鼠标监听器。

是否有人知道允许我保留工具提示和鼠标事件的变通方法?谢谢。

根据要求,这是我的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);
    }

}

2 个答案:

答案 0 :(得分:4)

这是一个解决方法:

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);
    }
}

答案 1 :(得分:1)

似乎没有任何问题(在Java 6下)。这是我的代码:

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);


            }
        });

    }


}