JTabbedPane.getTabComponentAt (int) returning null

I have the following code:

JTabbedPane container;
...
AWindow page = WinUtils.buildWindow();
boolean existing = checkIfExists(page); // in this code, this will always be false
if(!existing)
{
    String tabName = page.getLoadedFileLocation().getName();
    container.addTab(page.getLoadedFileLocation().getName(), page);
}
Component comp = container.getTabComponentAt(0);
int sel = container.getSelectedIndex();
container.setSelectedComponent(page);

the thing is this:

container.getTabComponentAt(0)

returns null. Another weird thing:

container.getSelectedIndex()

returns 0. The logical thing, which I think should happen, is to have a link to the created window. Why am I getting null? What am I doing wrong?

+3
source share
2 answers

getTabComponentAt()returns a custom component that you can add as a tab title. Perhaps you are looking for a method getComponentAt()to return the contents of a tab. getSelectedIndex()just returns that the first tab is selected (it will return -1 if no tabs are selected)

+15

JTabbedPane: .

getTabComponentAt(0) null, . , 0, , , , .

( Javadocs:

// In this case the look and feel renders the title for the tab.
tabbedPane.addTab("Tab", myComponent);
// In this case the custom component is responsible for rendering the
// title of the tab.
tabbedPane.addTab(null, myComponent);
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));

, , . , , , , .

.)

, getComponentAt(0).

+6

Source: https://habr.com/ru/post/1710371/


All Articles