Java 如何确定JTabbedPane中先前选择的选项卡?

Java 如何确定JTabbedPane中先前选择的选项卡?,java,swing,jtabbedpane,Java,Swing,Jtabbedpane,我使用的是JTabbedPane,希望在用户选择另一个选项卡时进行一些字段验证。根据他们对messagebox的回复,可能会将他们保留在同一选项卡上以更正错误数据。 我正在实现ChangeListener接口并处理stateChanged事件,但是getSelectedIndex返回新选择的选项卡。 有没有简单的方法 1确定之前选择的选项卡和 2如果数据验证失败,是否阻止用户转到新选择的选项卡?无法使用stateChanged事件。 提前感谢,您需要自己跟踪用户的选项卡。当用户导航到新选项卡时,

我使用的是JTabbedPane,希望在用户选择另一个选项卡时进行一些字段验证。根据他们对messagebox的回复,可能会将他们保留在同一选项卡上以更正错误数据。 我正在实现ChangeListener接口并处理stateChanged事件,但是getSelectedIndex返回新选择的选项卡。 有没有简单的方法 1确定之前选择的选项卡和 2如果数据验证失败,是否阻止用户转到新选择的选项卡?无法使用stateChanged事件。
提前感谢,

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


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

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


如果验证例程表明用户需要更正上一个选项卡中的某些数据,您可以在侦听器的stateChangedChangeEvent e方法中使用JTabbedPane.setSelectedIndexint index方法,将用户返回到以前选择的选项卡。

Hmm您可以尝试类似的方法,基本上添加一个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");
                }
            }
        });
    }
}

嗯,您可以尝试这样的方法,基本上是添加一个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");
                }
            }
        });
    }
}

请注意,仅当某些JComponents使用FocusOwner时,否则Property/ChangeListener将返回Nothing注意,仅当某些JComponents使用FocusOwner时,否则Property/ChangeListener将返回Nothing对于非模态行为,让相关选项卡共享对公共模型的访问。对于非模态行为,让相关选项卡共享对公共模型的访问。