getSelectedIndex()
そしてgetSelectedComponent()
動作するはずです。タブ付きペインのチュートリアルの使用方法を確認してください。良い例があります。
編集:getSelectedComponent
とのデモAbstractAction
import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
public class TabbedPaneDemo {
static class TextDemoPanel extends JPanel{
private JTextArea textArea;
public TextDemoPanel(String text){
textArea = new JTextArea(5, 20);
textArea.setText(text);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane);
}
public JTextArea getTextArea() {
return textArea;
}
}
static class SetTextAction extends AbstractAction {
private JTabbedPane tabbedPane;
public SetTextAction(JTabbedPane tabbedPane){
super("Set text");
this.tabbedPane = tabbedPane;
}
@Override
public void actionPerformed(ActionEvent e) {
String value = JOptionPane.showInputDialog(tabbedPane, "Text", "New text");
if (value != null){
TextDemoPanel panel = (TextDemoPanel)tabbedPane.getSelectedComponent();
if (panel != null)
panel.getTextArea().setText(value);
}
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TabbedPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab 1", new TextDemoPanel("Tab 1 text"));
tabbedPane.addTab("Tab 2", new TextDemoPanel("Tab 2 text"));
tabbedPane.addTab("Tab 3", new TextDemoPanel("Tab 3 text"));
frame.add(tabbedPane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
JMenuItem item = new JMenuItem(new SetTextAction(tabbedPane));
menu.add(item);
frame.setJMenuBar(menuBar);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}