I'm adding a custom component to a JTabbedPane
. The title of the tab is determined by getName()
in the component. Now at some point the result of getName()
changes, but the tab title is not automatically refreshed (as can be expected). How can I make it so?
Asked
Active
Viewed 237 times
3

mKorbel
- 109,525
- 20
- 134
- 319

Bart van Heukelom
- 43,244
- 59
- 186
- 301
1 Answers
3
Each time your component's name is changed, it could throw a PropertyChangeEvent
. When the component is added to the tabbed pane, you could add a PropertyChangeListener
to the component, listening for changes of its name
property, and updating the tab name accordingly.
Don't forget to remove the listener when the component is removed from the tabbed pane, though.

JB Nizet
- 678,734
- 91
- 1,224
- 1,255
-
"and updating the tab name accordingly." - What should I call for that? I'm not ever calling `setTitleAt()` on the pane right now, but the pane itself calls `getName()` on the component. Of course I could just call `setTitleAt()`, but is that correct? – Bart van Heukelom Feb 29 '12 at 11:23
-
Never mind, I've looked at the tabbed pane source. It simply calls `getName()` once, when adding the tab, and never after, so it seems I should just use `setTitleAt()`. – Bart van Heukelom Feb 29 '12 at 11:24
-
@Bart van Heukelom I saw that too, but I lost link and forgot about ***, but this issue was Look and Feel rellated, – mKorbel Feb 29 '12 at 11:29
-
@BartvanHeukelom sounds like the tabbedPane is a custom component as well? If so, the best place for the propertyChangeListener is that custom tabbedPane itself: let it register the listener to the component on insert and unregister on remove – kleopatra Feb 29 '12 at 11:40
-
@kleopatra No it's a default `JTabbedPane` – Bart van Heukelom Feb 29 '12 at 14:03