JTabbedPane Tab 组件的 MouseEvents 没有渗透

发布于 2024-09-08 05:02:30 字数 1082 浏览 6 评论 0原文

我有一个带有自定义选项卡组件的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汹涌人海 2024-09-15 05:02:33

我最近好像记得有一个这样的问题,虽然我找不到帖子了。我认为问题在于“自定义组件”接收鼠标事件,因此它不会传递到选项卡式窗格。建议的解决方案是使用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.

迷爱 2024-09-15 05:02:33

该问题与我进行更多挖掘后在此处发布的问题有关: SetToolTipText 的解决方法消耗鼠标事件?

The problem is related to the one that I posted here after I did more digging: Workaround for setToolTipText consuming mouse events?

岁月流歌 2024-09-15 05:02:32

我不记得在 TabComponentsDemo,在 如何使用选项卡式窗格。您可以将您的代码与该示例进行比较作为参考。

附录:重构 ButtonTabComponent 包含 getLabel(),此版本的 runTest()TabComponentsDemo 添加一个按钮来显示所需的行为。特别是,每次按下按钮时,选项卡都会重新绘制以显示放大的标题。

更新:在 pane.remove() 之后修改正确的选项卡组件。

public void runTest() {
    pane.removeAll();
    for (int i = 0; i < tabNumber; i++) {
        final int titleIndex = i;
        String title = "Tab " + titleIndex;
        final JButton button = new JButton("Relabel tab");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = pane.indexOfComponent(button);
                ButtonTabComponent btc = (ButtonTabComponent)
                    pane.getTabComponentAt(index);
                JLabel label = btc.getLabel();
                pane.setTitleAt(index, label.getText() + titleIndex);
                label.invalidate();
                pane.repaint();
            }
        });
        pane.add(title, button);
        initTabComponent(i);
    }
    tabComponentsItem.setSelected(true);
    pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
    scrollLayoutItem.setSelected(false);
    this.setPreferredSize(new Dimension(500, 200));
    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

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 include getLabel(), this version of runTest() in TabComponentsDemo 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().

public void runTest() {
    pane.removeAll();
    for (int i = 0; i < tabNumber; i++) {
        final int titleIndex = i;
        String title = "Tab " + titleIndex;
        final JButton button = new JButton("Relabel tab");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = pane.indexOfComponent(button);
                ButtonTabComponent btc = (ButtonTabComponent)
                    pane.getTabComponentAt(index);
                JLabel label = btc.getLabel();
                pane.setTitleAt(index, label.getText() + titleIndex);
                label.invalidate();
                pane.repaint();
            }
        });
        pane.add(title, button);
        initTabComponent(i);
    }
    tabComponentsItem.setSelected(true);
    pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
    scrollLayoutItem.setSelected(false);
    this.setPreferredSize(new Dimension(500, 200));
    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文