Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

container.getTabComponentAt(0)

returns null. The other weird thing is :

container.getSelectedIndex()

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

like image 642
Geo Avatar asked Jun 12 '09 19:06

Geo


2 Answers

getTabComponentAt() returns the custom component you might add as the tab title. You might be looking for the getComponentAt() method to return the contents of a tab. The getSelectedIndex() just returns that the first tab is currently selected (it would return -1 for no tabs selected)

like image 147
akarnokd Avatar answered Oct 12 '22 23:10

akarnokd


You're confusing the two sets of methods in JTabbedPane: the tab component methods and the component methods.

getTabComponentAt(0) is returning null because you haven't set the tab component. You've set the component that is displayed at index 0, but the tab component is the component that renders the tab--not the component that displays in the pane.

(Notice the example in the 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"));

The latter is typically used when you want a more complex user interaction that requires custom components on the tab. For example, you could provide a custom component that animates or one that has widgets for closing the tab.

Normally, you won't need to mess with tab components.)

Anyway, try getComponentAt(0) instead.

like image 43
Michael Myers Avatar answered Oct 13 '22 00:10

Michael Myers