Question

I have a JFrame extended class that implements a multi-tab chat. Every tab is a chat with someone or with a group of people. What I have implemented works fine, except when I assign a ToolTipText to the label of a tab. In this case I can't click anymore (and select) the tab that has a ToolTipText assigned. The others work fine.

Graphical example: enter image description here

As you can see the tabs are properly being added, and the first two tabs ("Gruppo prova" and "Gruppo test") have a ToolTipText, the other two don't. I can switch between the last two, but I can't do the same with the first two. I thought that the icon next to the label could be a problem, but I removed it and still doesn't work. However I can still click all the 'X' (close) buttons (working properly).

This is a piece of the code I used to add a tab:

        // Some stuff...
        JChat chat = new JChat(gui.chatClient, email, name, group);
        jTabbedPane.add(email, chat); // I instantiated this before

        int index = jTabbedPane.indexOfTab(email);
        JPanel pnlTab = new JPanel(new GridBagLayout());
        pnlTab.setOpaque(false);

        // Core function
        JLabel lblTitle;

        if (group == 1) {
        // If it's a group and not a single chat I assign a name, an icon and a ToolTipText to the tab
            lblTitle = new JLabel(name, icon, JLabel.LEFT);
            lblTitle.setToolTipText(membersList.toString());
        } else {
        // otherwise I only assign a name to the tab
            lblTitle = new JLabel(name);
        }

        jTabbedPane.setTabComponentAt(index, pnlTab);

        // This applies the 'X' (close) button next to the tab name
        CloseButton btnClose = new CloseButton(this, jTabbedPane, tabs, email);

        lblTitle.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

        pnlTab.add(lblTitle);
        pnlTab.add(btnClose);

Is this a Swing bug or am I doing something wrong?

Was it helpful?

Solution

you can use :

  1. void setToolTipTextAt(int, String) to set tool-tip text to specific tab.
  2. void setIconAt(int index, Icon icon) to set the icon to specific tab.

No need to use JLabel for setting tool-tip text or icon.

The above solution, however doesn't however answer your question:

except when I assign a ToolTipText to the label of a tab. In this case I can't click anymore (and select) the tab that has a ToolTipText assigned

The only reason i am suspecting:

JLabel doesn't register to any mouse listener by default. When no mouse listener is set to JLabel any mouse clicked event will go through to the UI objects underneath: in this case the JTabbedPane. But when we are setting tool-tip text using setToolTipText(text), the ToolTipManger adds a mouse listener to this JLabel, which will continue to consume the mouse click event.

Check the following code snippets demonstrating the issue and providing a work around setSelectedIndex function:

    JLabel label = new  JLabel("a Label");
    System.out.println(label.getMouseListeners().length); // length is printed as 0

    label.setToolTipText("Danger: setting tool tip will consume mouse event");
    System.out.println(label.getMouseListeners().length); // length is printed as 1

    jTabbedPane1.setTabComponentAt(0, label);

    label.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
           int index =  jTabbedPane1.indexOfTabComponent((Component)e.getSource());
           jTabbedPane1.setSelectedIndex(index);
        }

    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top