I have a JTabbedPane with a custom tab that displays a filename, followed by a button. When The button is pressed, I would like to close that tab. My first thought was that (hopefully) when I click on the button, it would first switch to that tab, then I'd be able to just close the tab at that index. However that is not the case. I'm looking for a way to retrieve the tab index that the button is on, then I could close that tab. I tried researching and came up with indexOfComponent
, but I'm unsure of how to use it in my situation. My tabs are created dynamically, therefore I store stuff inside of a vector, that creates JPanels, then displays the current information based on the selected index. (tab 0 would have all its components inside of vec[0], tab 1 = vec[1], etc).
My TextEdit class is setup like this:
public class TextEdit extends JTabbedPane {
private Vector<JTextArea> vec; // User can make and edit files
private Vector<JPanel> tabPanel; // Panel that has a JLabel for fileName and JButton to close file
private Vector<JLabel> absolutePath; // Path to which file will be saved
public TextEdit() {
// Sets up actionlisteners, initializes variables, etc
}
protected void AddTab(String fileName) {
JPanel panel = new JPanel; // Where textarea will be
panel.setLayout(new GridLayout(0,1));
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
panel.add(sp);
JPanel tp = new JPanel(); // The tab panel
tp.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 2));
JLabel label = new JLabel(fileName);
JButton button = new JButton("X");
button.addActionListener(CloseOne); // My Action that will close the file
tp.add(label);
tp.add(button);
this.addTab(fileName, panel); // Sets the text area into the tab
this.setTabComponentAt(this.getSelectedIndex, tp); // Sets the tab to the fileName and button
vec.add(ta); // Add textarea to vector for future use
tabPanel.add(tp); // Add the tabpanel for future use. this is where the button is, I need to setSelectedIndex to the tab that the button resides onclick
}
TextEditor.java
Action CloseOne = new AbstractAction("CloseOne") {
@Override
public void actionPerformed(ActionEvent e) {
// This is where I need to set the selected tab to the tab in which the button was pressed.
}
};
The TextEditor class is what has all my actions in, and hold a TextEdit (where the tabs, etc are at).
My textedit has methods to set and get the selected index.
Going back to indexOfComponent, I would need to specify which component I'm using, however, my components are added dynamically, and I'd have to figure out the selectedindex to get into the vector or JPanels to even use that. Or am I missing something? Any suggestions?