Click here to Skip to main content
15,885,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have JTabbedPane that consists of multiple JPanels who are dynamically made based on a text file. Is there any way to get the caption of selected JTabbedPane?
I've tried

Java
getTabbedPane.getSelectedComponent().getName())


but it doesn't work, as it gives back NULL.

What I have tried:

I've tried Google'ing it, and using .getSelectedComponent().getName
Posted
Updated 20-Jul-21 23:06pm
v2
Comments
Richard MacCutchan 21-Jul-21 3:43am    
Which part returns null and what is the currently selected component? Perhaps showing a bit more of your code would help.

1 solution

The getName method returns the string that is set by setName, which is not the text of the component. You need to get the index of the selected tab from JTabbedPane.getSelectedIndex (Java Platform SE 8 )[^] and then use JTabbedPane.getTitleAt (Java Platform SE 8 )[^].
 
Share this answer
 
Comments
Just Caused 21-Jul-21 8:40am    
That seems to work! I've been searching the answer and trying various different ways, but to be honest I haven't though about this simple method. Thank you!
Richard MacCutchan 21-Jul-21 9:26am    
I found it by looking at the documentation.
Maciej Los 21-Jul-21 9:37am    
5ed!
Richard MacCutchan 21-Jul-21 9:38am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900