JTabbedPane: Change Title from within Tab
Asked Answered
A

3

5

I'm trying now for a couple of hours to optimize my user interface but I'm not getting any further right now. I got a JTabbedPane to show datasets. There is one textfield in there with should also represent the tabs title. Right now there is a button labeled "save" which does nothing else but read this text field from the current tab and updates the tabs title. I'd love to replace this by updating the tab's title when the field is changed. I got the event listener up and running, so no problems here, but how to I get to call the JTabbedPane object? I tried to put a JTabbedPane variable into my JPanel class and store a reference here, but this keeps crashing the moment I call the setter for this variable... Well, not actually crashing, but it throws an exception:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411) 
[...]

The setter is quite simple:

public void setTabContainer(JTabbedPane cont){
    container = cont;
 }

Any ideas?

Acceptable answered 13/1, 2015 at 15:36 Comment(1)
The stack trace and the method you have posted are not related in any way. That should become obvious to you as there is no mentioning of your method in that stack trace. Look at the part of the stack trace you have omitted to find the method causing the problem.Gyron
S
7

I got the event listener up and running, so no problems here, but how to I get to call the JTabbedPane object?

You can use SwingUtilities class as follows to get the tabbed pane that is the ancestor of your text field:

JTabbedPane tabbedPane = (JTabbedPane)SwingUtilities.getAncestorOfClass(JTabbedPane.class, textField);

Then you can iterate over the tabbed pane's components in order to find the index where your text field is placed and finally update the tab's title:

for(int i = 0; i < tabbedPane.getTabCount(); i++) {
    if(SwingUtilities.isDescendingFrom(textField, tabbedPane.getComponentAt(i))) {
        tabbedPane.setTitleAt(i, textField.getText());
        break;
    }
}

See the API for:

Selfstyled answered 13/1, 2015 at 15:52 Comment(0)
G
1

Try:

JTabbedPane tabbedPane = (JTabbedPane) SwingUtilities.getAncestorOfClass(JTabbedPane.class, this);
tabbedPane.setTitleAt(tabbedPane.indexOfTabComponent(this), title);

Assumes that this is the tab component, and title is the new title. Note that you must have set this as the content for the tab.

Uses:

Goldbrick answered 4/11, 2017 at 21:44 Comment(0)
A
0

In IntelliJ IDEA's form designer each tab is a JPanel hence you can use the following method to set the title from within Java code:

public static void setTabTitle(JPanel tab, String title)
{
    JTabbedPane tabbedPane = (JTabbedPane) SwingUtilities.getAncestorOfClass(JTabbedPane.class, tab);

    for (int tabIndex = 0; tabIndex < tabbedPane.getTabCount(); tabIndex++)
    {
        if (SwingUtilities.isDescendingFrom(tab, tabbedPane.getComponentAt(tabIndex)))
        {
            tabbedPane.setTitleAt(tabIndex, title);
            break;
        }
    }
}

This solution is very similar to the one given by dic19 though.

Anaplastic answered 29/6, 2017 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.