1

I'm trying building my tab object close action and for that I'm thinking some ways to do it. The easiest (until now I think so) it's to remove the tab component through the tab index like:

tabcomponent.remove(int index);

but my problem is, I don't know how to get the tab I'm focused on the application (such as returning this focused tab as an int) and pass this to the index to continue the close operation. I found nothing about this problem researching on internet and I would like to know if someone else has another opinion or idea to solve this? thanks in advance

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77
  • 2
    Have you tried getSelectedIndex()? – kiheru Jul 15 '13 at 07:17
  • please question is about how to remove some, any all ... JComponent from visible Tab or remove viosible Tab – mKorbel Jul 15 '13 at 07:20
  • 1
    *"I found nothing about this problem researching on internet"* - Really, take a look at [this example](http://stackoverflow.com/questions/11553112/how-to-add-close-button-to-a-jtabbedpane-tab/11553266#11553266) – MadProgrammer Jul 15 '13 at 07:20
  • @kiheru This assumes (and from what I've read I think you're right to do so) that the OP only wants to close the currently selected tab. – MadProgrammer Jul 15 '13 at 07:21
  • Thanks for the support guys, but now that you mentioned, if it's to close one tab that is not select, how that should work? I should set an event and pass the tab component I want to close? – Victor Oliveira Jul 15 '13 at 07:43

1 Answers1

3

Use getSelectedIndex() to find out the currently open tab:

int index = tabcomponent.getSelectedIndex();
if (index != -1) {
    tabcomponent.remove(index);
}
kiheru
  • 6,588
  • 25
  • 31
  • thanks, thats really usefull, but now that people mentioned up there Im wondering how that would work for the non selecteds, check out my comment up there to have a better idea of what Im talking about – Victor Oliveira Jul 15 '13 at 07:44
  • You might keep a list of the names (perhaps shown in a `JList`) and call [`JTabbedPane.getTitleAt(int)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#getTitleAt%28int%29) to determine which tabs they match up to. Other than that, how does the app. know which one(s) the user wants to close (*besides* using `java.telepathy.*`)? OTOH that is perhaps a topic for a new question. ;) – Andrew Thompson Jul 15 '13 at 10:05