you can use :
void setToolTipTextAt(int, String)
to set tool-tip text to specific tab.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);
}
});