JTabbedPane.getTabComponentAt(int) 返回 null
我有以下代码:
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);
事情是:
container.getTabComponentAt(0)
返回null
。 另一个奇怪的事情是:
container.getSelectedIndex()
返回0
。 我认为应该发生的合乎逻辑的事情是引用创建的窗口。 为什么我收到 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getTabComponentAt()
返回您可能添加为选项卡标题的自定义组件。 您可能正在寻找getComponentAt()
方法来返回选项卡的内容。 getSelectedIndex() 仅返回当前选择的第一个选项卡(如果没有选择选项卡,则返回 -1)getTabComponentAt()
returns the custom component you might add as the tab title. You might be looking for thegetComponentAt()
method to return the contents of a tab. ThegetSelectedIndex()
just returns that the first tab is currently selected (it would return -1 for no tabs selected)您混淆了 JTabbedPane 中的两组方法:选项卡组件方法和组件方法。
getTabComponentAt(0)< /code>
返回
null
,因为您尚未设置选项卡组件。 您已设置在索引 0 处显示的组件,但选项卡组件是呈现选项卡的组件,而不是在窗格中显示的组件。(请注意 Javadocs 中的示例:
通常,您不需要弄乱选项卡组件。)
无论如何,请尝试
getComponentAt(0)
代替。You're confusing the two sets of methods in
JTabbedPane
: the tab component methods and the component methods.getTabComponentAt(0)
is returningnull
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:
Normally, you won't need to mess with tab components.)
Anyway, try
getComponentAt(0)
instead.