Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTabbedPane: Change Title from within Tab

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?

like image 763
datmw Avatar asked Jan 13 '15 15:01

datmw


People also ask

How do I add components to JTabbedPane?

In Swing, JTabbedPane is simply a specialized container. Each tab has a name. To add a tab to the JTabbedPane , simply call addTab() . You'll need to specify the name of the tab as well as a component that supplies the tab's contents.

How do I hide tabs in JTabbedPane?

To disable a tab in a JTabbedPane container, use the setEnabledAt() method and set it to false with the index of the tab you want to disable.

Which method is used to add tabs to a JTabbedPane?

To Create Tabbed Panes. To create a tabbed pane, instantiate JTabbedPane , create the components you wish it to display, and then add the components to the tabbed pane using the addTab method.


2 Answers

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:

  • SwingUtilities#getAncestorOfClass(Class c, Component comp)
  • SwingUtilities#isDescendingFrom(Component a, Component b)
  • JTabbedPane#getComponentAt(int index)
like image 73
dic19 Avatar answered Sep 29 '22 17:09

dic19


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:

  • JTabbedPane#indexOfTabComponent
  • JTabbedPane#setTitleAt
  • SwingUtilities#getAncestorOfClass
like image 44
Solomon Ucko Avatar answered Sep 29 '22 16:09

Solomon Ucko