Get the index of the tab by matching the child component in JTabbedPane in Java
Description
The following code shows how to get the index of the tab by matching the child component in JTabbedPane.
Example
// w w w . j a va2 s. c o m
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Tabbed Pane Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
JButton button = new JButton("a");
tabbedPane.addTab("java2s.com", new JButton("a"));
tabbedPane.addTab("java2s.com", button);
int index = tabbedPane.indexOfComponent(button);
System.out.println(index);
frame.add(tabbedPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »