MouseEvents for a JTabbedPane Tab component are not bleeding through

后端 未结 3 1762
走了就别回头了
走了就别回头了 2021-01-21 07:13

I have a JTabbedPane with a custom tab component. That component contains a JLabel (To display the tab title) and a JButton (A close butto

相关标签:
3条回答
  • 2021-01-21 07:40

    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);
    }
    
    0 讨论(0)
  • 2021-01-21 07:55

    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.

    0 讨论(0)
  • 2021-01-21 07:55

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

    0 讨论(0)
提交回复
热议问题