user592704
user592704

Reputation: 3704

JTabbedPane - addTab(title, icon, component) + setTabComponentAt(index, component)

According to this example to be more detailed this one I just faced a strange thing...

If I am using method as addTab(title,icon,component) and next want to add additional component with setTabComponent(count-1,aComponent) then aComponent replaces icon? I mean in this case tab does not contain icon :(

The effect is something like (see image). But how to add icons on such tabs?

enter image description here

I am not pretty sure but I guess somehow aComponent replaces icon... So my question is... How to have three of them icon , title and aComponent on one tab at the same time?

Upvotes: 1

Views: 1621

Answers (2)

Robin
Robin

Reputation: 36621

As clearly mentioned in the javadoc of setTabComponentAt the component you set at the tab will replace the icon and the title

A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon.

So if you want to have a title, icon and a component on the tab you will have to create your own Container containing those 3 items, and set that container using the setTabComponentAt method.

Upvotes: 2

Nick Rippe
Nick Rippe

Reputation: 6475

I think you meant to use setComponentAt instead of setTabComponent.

You see, setTabComponent sets a component in charge of rendering the tabs (I'm guessing that's not what you want). setComponentAt sets the component in the specified tab (I'm guessing this is what you're looking for).

In summary, to add a tab that has a title and icon, and contains a component in the tab body, use: addTab(title, icon, component)

To update (or add) a component to an existing tab, use: setComponentAt(index, component)

To update (or add) an icon to an existing tab, use: setIconAt(index, icon)

To update (or add) a title to an existing tab, use: setTitleAt(index, title)

Upvotes: 1

Related Questions