2014-11-29 23:55:07

JTabbedPane 监听tab选中改变事件

利用的是javax.swing.event.ChangeListener,原文Listening for Selected Tab Changes。实现方式都是callback,不过在实际当中为了方便我选择了自己写callback。
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TabSample {
	static void add(JTabbedPane tabbedPane, String label) {
		JButton button = new JButton(label);
		tabbedPane.addTab(label, button);
	}
	public static void main(String args[]) {
		JFrame frame = new JFrame("Tabbed Pane Sample");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JTabbedPane tabbedPane = new JTabbedPane();
		String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
		for (int i = 0, n = titles.length; i < n; i++) {
			add(tabbedPane, titles[i]);
		}
		ChangeListener changeListener = new ChangeListener() {
			public void stateChanged(ChangeEvent changeEvent) {
				JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
				int index = sourceTabbedPane.getSelectedIndex();
				System.out.println("Tab changed to: "+ sourceTabbedPane.getTitleAt(index));
			}
		};
		tabbedPane.addChangeListener(changeListener);
		frame.add(tabbedPane, BorderLayout.CENTER);
		frame.setSize(400, 150);
		frame.setVisible(true);
	}
}
发表回复