如何确定JTabbedPane中先前选择的Tab?

时间:2012-06-15 11:46:59

标签: java swing jtabbedpane

我正在使用JTabbedPane,并希望在用户选择另一个Tab时进行一些字段验证。可能,根据他们对消息框的回复,将它们保存在同一选项卡上以纠正错误数据。 我正在实现ChangeListener接口并处理stateChanged事件,但getSelectedIndex()返回新选择的选项卡。 有没有简单的方法 1)确定哪个是先前选择的Tab和 2)在数据验证失败的情况下,阻止用户转到新选择的Tab?无法使用stateChanged事件。 提前谢谢,

2 个答案:

答案 0 :(得分:5)

您需要跟踪用户自己的标签。当用户导航到新选项卡时,您更新一个int字段以指示用户已打开的选项卡。

如果验证例程表明用户需要更正上一个选项卡中的某些数据,则可以在监听器的JTabbedPane.setSelectedIndex(int index) - 方法中使用stateChanged(ChangeEvent e) - 方法将用户返回到先前选择的选项卡。

答案 1 :(得分:3)

嗯,你可以试试这样的东西,基本上是添加一个ChangeListener来监控当前标签并保留以前的标签选择:

public class JTabbedPaneExample extends JFrame {

    private JTabbedPane tabbedPane;
    private JPanel panel1, panel2, panel3;
    private int currentTabIndex = 0, previousTabIndex = 0;

    public JTabbedPaneExample() {
        createAndShowUI();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JTabbedPaneExample jTabbedPaneExample = new JTabbedPaneExample();
            }
        });
    }

    private void createAndShowUI() {
        setTitle("JTabbedPaneExample");
        setSize(300, 300);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        addComponentsToContentPane(getContentPane());
        addListeners();
        //pack();
        setVisible(true);
    }

    private void addComponentsToContentPane(Container contentPane) {
        tabbedPane = new JTabbedPane();
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        tabbedPane.insertTab("tab1", null, panel1, null, 0);
        tabbedPane.insertTab("tab2", null, panel2, null, 1);
        tabbedPane.insertTab("tab3", null, panel3, null, 2);
        contentPane.add(tabbedPane);
    }

    private void addListeners() {
        tabbedPane.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent ce) {
                previousTabIndex = currentTabIndex;
                currentTabIndex = tabbedPane.getSelectedIndex();
                System.out.println("Current tab is:" + currentTabIndex);
                System.out.println("Previous tab is:" + previousTabIndex);
            }
        });
    }
}

编辑:这是一个较新的版本,如果验证变量设置为'true',只允许更改标签,这是通过按钮点击来完成的,只是为了显示逻辑:

public class JTabbedPaneExample extends JFrame {

    private JTabbedPane tabbedPane;
    private JPanel panel1, panel2, panel3;
    private int currentTabIndex = 0, previousTabIndex = 0;
    private boolean valid = false;
    private JButton changeVariableBtn;

    public JTabbedPaneExample() {
        createAndShowUI();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JTabbedPaneExample jTabbedPaneExample = new JTabbedPaneExample();
            }
        });
    }

    private void createAndShowUI() {
        setTitle("JTabbedPaneExample");
        setSize(300, 300);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        addComponentsToContentPane(getContentPane());
        addListeners();
        //pack();
        setVisible(true);
    }

    private void addComponentsToContentPane(Container contentPane) {
        tabbedPane = new JTabbedPane();
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        changeVariableBtn = new JButton("Set validation to true");
        tabbedPane.insertTab("tab1", null, panel1, null, 0);
        tabbedPane.insertTab("tab2", null, panel2, null, 1);
        tabbedPane.insertTab("tab3", null, panel3, null, 2);
        contentPane.add(tabbedPane, BorderLayout.NORTH);
        contentPane.add(changeVariableBtn, BorderLayout.CENTER);
    }

    private void addListeners() {
        tabbedPane.addChangeListener(new ChangeListener() {

            private boolean automatedStateChange = false;

            @Override
            public void stateChanged(ChangeEvent ce) {
                //this is used so when we allow the user not to go to the new tab by setting the tabb index to the previous one we dont want our changelistener to fire again as if the user were changing the tabs
                if (automatedStateChange) {
                    automatedStateChange = false;
                } else {
                    previousTabIndex = currentTabIndex;
                    currentTabIndex = tabbedPane.getSelectedIndex();
                }

                if (valid) {
                    System.out.println("Current tab is:" + currentTabIndex);
                    System.out.println("Previous tab is:" + previousTabIndex);
                    System.out.println("Validation succeeded: " + valid);
                    changeVariableBtn.doClick();
                } else {
                    System.out.println("You need to enter all valid data first!");
                    automatedStateChange = true;
                    tabbedPane.setSelectedIndex(previousTabIndex);
                }
            }
        });
        changeVariableBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (valid) {
                    valid = false;
                    changeVariableBtn.setText("Set validation to true");
                } else {
                    valid = true;
                    changeVariableBtn.setText("Set validation to false");
                }
            }
        });
    }
}