views:

159

answers:

3

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;
    }
  }
+1  A: 

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);
}
trashgod
That was the example that my code was originally based on. The problem is that the JLabel that they used displayed the text by overriding the getText() method in the JLabel class. While this doesn't have the mouseclick issue,it has the problem of not resizing the tab when the text is changed. This ends up giving me tabs with cutoff titles. Is there a solution for that?
Sandro
@Sandro: My first thought would be `Container` `validate()`.
trashgod
@Sandro: Looking closer at `TabComponentsDemo`, an implicit `validate()` occurs as a result of the call to `setVisible()` in `runTest()`.
trashgod
I'm still getting the same behavior even with the validate.
Sandro
@Sandro: Any chance you're inadvertently replacing the label, rather than modifying it?
trashgod
I ended up figuring out that the problem is related to the issue that I posted here: http://stackoverflow.com/questions/3198037/workaround-for-settooltiptext-consuming-mouse-events
Sandro
@Sandro: I've updated the example for anyone else who might need it. It seems to handle tool tips correctly.
trashgod
+1  A: 

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 solutin suggested was to use the dispatchEvent(...) methdo to redispatch the mouse event to the proper tab.

camickr
Oh ok this sounds close. Who can I dispatch it to so that it goes to the right place?
Sandro
The tabbed pane.
camickr
I tried it and still nothing.
Sandro
A: 

The problem is related to the one that I posted here after I did more digging: http://stackoverflow.com/questions/3198037/workaround-for-settooltiptext-consuming-mouse-events

Sandro