消耗鼠标事件的setToolTipText的解决方法? [英] Workaround for setToolTipText consuming mouse events?
本文介绍了消耗鼠标事件的setToolTipText的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这似乎是SWING验证的问题
This seems to be a verified problem with SWING
http://forums.sun.com/thread.jspa?threadID=385730
我正在尝试设置 JTabbedPane
中选项卡的工具提示文本,但是当我这样做时,我实际上无法再选择该选项卡,因为工具提示添加了一个消耗事件的鼠标侦听器。
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.
根据要求,这里是我的SSCCE
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);
}
}
推荐答案
以下是一种解决方法:
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);
}
}
这篇关于消耗鼠标事件的setToolTipText的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文