JTabbedPane: how best to identify clicked tab

S

Scott Steiner

Hi,

I would like to identify which tab of my JTabbedPane has been selected.
Currently I know of 2 methods:

a) by getSelectedIndex()
i = myTabbedPane.getSelectedIndex()
if (i == 0) ...

b) by getTitleAt()
tabTitle = myTabbedPane.getTitleAt(myTabbedPane.getSelectedIndex())
if (tabTitle.equals(...))

I can't use getSelectedIndex() because my TabbedPane is dynamic and the
index can change. Hence I use the getTitleAt() method. It seems to me a
little strange though to identify a tab by its string title. Is there no
way one can assign a sort of unique ID to a tab which I can query?

Thanks in advance.
 
P

Paul van Rossem

Hi,

I would like to identify which tab of my JTabbedPane has been selected.
Currently I know of 2 methods:

a) by getSelectedIndex()
i = myTabbedPane.getSelectedIndex()
if (i == 0) ...

b) by getTitleAt()
tabTitle = myTabbedPane.getTitleAt(myTabbedPane.getSelectedIndex())
if (tabTitle.equals(...))

I can't use getSelectedIndex() because my TabbedPane is dynamic and the
index can change. Hence I use the getTitleAt() method. It seems to me a
little strange though to identify a tab by its string title. Is there no
way one can assign a sort of unique ID to a tab which I can query?

Thanks in advance.
You can always use the tab itself as an identification:
mtTabbedPane.addTab(SomePane, ...);
if (myTabbedPane.getComponent(index) == SomePane) ...

paul
 
L

LDB

if i understand you correctly ... you want to set this ...

tabbedPane.setBackgroundAt(4, unselectedColor);
tabbedPane.setForegroundAt(4, selectedColor);


LDB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top