JTabbedPane Tab 组件的 MouseEvents 没有渗透
我有一个带有自定义选项卡组件的 JTabbedPane
。该组件包含一个 JLabel
(用于显示选项卡标题)和一个 JButton
(关闭按钮)。当我更改 JLabel
中的文本时,JLabel
停止接收鼠标事件,并且当我直接单击标签而不是单击标签周围时,我无法再选择该选项卡然后我可以选择该选项卡。有什么想法吗?
代码片段:
class ShellPanelTabComponent extends JPanel implements ActionListener{
private ShellPanel panel;
private JLabel label;
public ShellPanelTabComponent(final ShellPanel panel){
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.panel = panel;
setOpaque(false);
label = new JLabel(panel.getTitle());
label.setFocusable(false);
add(label);
label.setBorder(BorderFactory.createEmptyBorder(2,0,0,5));
//now the button
CloseButton closeButton = new CloseButton(panel);
add(closeButton);
closeButton.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
panel.getShell().removeShellPanel(panel);
}
/**
* @return the label
*/
public JLabel getLabel() {
return label;
}
}
I have a JTabbedPane
with a custom tab component. That component contains a JLabel
(To display the tab title) and a JButton
(A close button). When I change the text in the JLabel
the JLabel
stops receiving mouse events and I can no longer select that tab when I click directly on the label instead if I click around the label then I can select the tab. Any ideas?
A snippet of the code:
class ShellPanelTabComponent extends JPanel implements ActionListener{
private ShellPanel panel;
private JLabel label;
public ShellPanelTabComponent(final ShellPanel panel){
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.panel = panel;
setOpaque(false);
label = new JLabel(panel.getTitle());
label.setFocusable(false);
add(label);
label.setBorder(BorderFactory.createEmptyBorder(2,0,0,5));
//now the button
CloseButton closeButton = new CloseButton(panel);
add(closeButton);
closeButton.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
panel.getShell().removeShellPanel(panel);
}
/**
* @return the label
*/
public JLabel getLabel() {
return label;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近好像记得有一个这样的问题,虽然我找不到帖子了。我认为问题在于“自定义组件”接收鼠标事件,因此它不会传递到选项卡式窗格。建议的解决方案是使用dispatchEvent(...)方法将鼠标事件重新分派到正确的选项卡。
I seem to remember a question like this recently although I can't find the posting. I believe the problem is that the "custom component" receives the mouse event so it is not passed on to the tabbed pane. The solution suggested was to use the dispatchEvent(...) method to redispatch the mouse event to the proper tab.
该问题与我进行更多挖掘后在此处发布的问题有关: SetToolTipText 的解决方法消耗鼠标事件?
The problem is related to the one that I posted here after I did more digging: Workaround for setToolTipText consuming mouse events?
我不记得在
TabComponentsDemo
,在 如何使用选项卡式窗格。您可以将您的代码与该示例进行比较作为参考。附录:重构
ButtonTabComponent
包含getLabel()
,此版本的runTest()
在TabComponentsDemo
添加一个按钮来显示所需的行为。特别是,每次按下按钮时,选项卡都会重新绘制以显示放大的标题。更新:在
pane.remove()
之后修改正确的选项卡组件。I don't recall seeing such a problem in the
TabComponentsDemo
, discussed in How to Use Tabbed Panes. You might compare your code with that example as a reference.Addendum: Re-factoring
ButtonTabComponent
to includegetLabel()
, this version ofrunTest()
inTabComponentsDemo
adds a button that evinces the desired behavior. In particular, each time the button is pressed, the tabs are redrawn to display the enlarged title.Update: Modify correct tab component after
pane.remove()
.