Java 如何防止在JTabbedPane上留下选项卡

Java 如何防止在JTabbedPane上留下选项卡,java,swing,jtabbedpane,Java,Swing,Jtabbedpane,我有一个应用程序,它有嵌套的JTabbedPane。每个JTabbedPane包括其他JTabbedPane 我想在允许用户离开当前选项卡之前检查一些内容 当您运行下面的代码时,您将看到月份->二月选项卡有一个允许复选框 如果选中“允许”复选框,则我希望允许用户导航以离开当前面板。如果不是,那么他们不能离开,直到它 在显示下一个组件(可能在另一个JTabbedPane上)之前,我似乎找不到处理离开请求的方法 这可以通过转到月份->二月选项卡,然后选择颜色选项卡来演示 有什么想法吗 import

我有一个应用程序,它有嵌套的JTabbedPane。每个JTabbedPane包括其他JTabbedPane

我想在允许用户离开当前选项卡之前检查一些内容

当您运行下面的代码时,您将看到月份->二月选项卡有一个允许复选框

如果选中“允许”复选框,则我希望允许用户导航以离开当前面板。如果不是,那么他们不能离开,直到它

在显示下一个组件(可能在另一个JTabbedPane上)之前,我似乎找不到处理离开请求的方法

这可以通过转到月份->二月选项卡,然后选择颜色选项卡来演示

有什么想法吗

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class VerifyBeforeLeavingTab extends JFrame
{
    private static final long   serialVersionUID    = 1L;

    public VerifyBeforeLeavingTab()
    {
        setSize(700, 700);

        JTabbedPane mainTabbedPane = new JTabbedPane(JTabbedPane.TOP);

        MyJTabbedPane colorsPane = new MyJTabbedPane(JTabbedPane.TOP, "colors");
        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.blue);
        colorsPane.addTab("Blue", bluePanel);

        JPanel redPanel = new JPanel();
        redPanel.setBackground(Color.red);
        colorsPane.addTab("Red", redPanel);

        mainTabbedPane.addTab("Colors", colorsPane);

        JTabbedPane monthsPane = new MyJTabbedPane(JTabbedPane.TOP, "months");

        JPanel janPanel = new JPanel();
        monthsPane.addTab("January", janPanel);

        JPanel febPanel = new MyUnleavableJPanel();
        monthsPane.addTab("February", febPanel);

        mainTabbedPane.addTab("Months", monthsPane);

        add(mainTabbedPane, BorderLayout.CENTER);

        getContentPane().add(mainTabbedPane);
    }

    private class MyUnleavableJPanel extends JPanel
    {
        private static final long   serialVersionUID    = 1L;

        public MyUnleavableJPanel()
        {
            final JCheckBox chckBoxAllowToLeave = new JCheckBox("Allow to leave");
            chckBoxAllowToLeave.setBounds(100, 100, 50, 50);
            this.add(chckBoxAllowToLeave);

            addHierarchyListener(new HierarchyListener()
            {
                public void hierarchyChanged(HierarchyEvent e)
                {
                    if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) != 0)
                    {
                        if (isShowing())
                        {
                            System.out.println("Showing an unleavable panel");
                        }
                        else
                        {
                            // TODO: Do not let them leave this JCheckbox is selected
                            if (chckBoxAllowToLeave.isSelected())
                            {
                                System.out.println("OK to leave");
                            }
                            else
                            {
                                System.out.println("Not allowed to leave");
                            }
                        }
                    }
                }
            });
        }
    }

    private class MyJTabbedPane extends JTabbedPane
    {
        private static final long   serialVersionUID    = 1L;

        public MyJTabbedPane(int i, String name)
        {
            super(i);
            setName(name);
        }

        @Override
        public void setSelectedIndex(int index)
        {
            System.out.println("Now on '" + getName() + "' tab #" + index);
            super.setSelectedIndex(index);
        }
    }

    private static void createAndShowGUI()
    {
        // Create and set up the window.
        VerifyBeforeLeavingTab frame = new VerifyBeforeLeavingTab();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    createAndShowGUI();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    System.exit(0);
                }
            }
        });
    }

}

我扩展了JTabbedPane并重写了setSelectedIndex()。如果要允许选择成功,请调用super.setSelectedIndex;否则不要


我使用它调用与该选项卡关联的验证例程,如果验证失败,则否决更改。这种方法的一个优点是,它完全不依赖于用户如何更改选项卡——他可以单击选项卡,单击移动选项卡的按钮,等等。

我扩展了JTabbedPane并覆盖了setSelectedIndex()。如果要允许选择成功,请调用super.setSelectedIndex;否则不要


我使用它调用与该选项卡关联的验证例程,如果验证失败,则否决更改。这种方法的一个优点是,它完全不依赖于用户如何更改选项卡——他可以单击选项卡,单击移动选项卡的按钮,等等。

好的,决定继续我自己的示例;两个类,运行main类会生成一个swing窗口,允许您每隔单击一次就更改选项卡

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class TabbedPaneExample extends JFrame
    {
        public static void main(String[] args)
        {
            TabbedPaneExample example = new TabbedPaneExample();
            example.go();
        }

        public void go()
        {
            ValidatingTabbedPane vtp = new ValidatingTabbedPane();
            for(int i=0; i<3; i++)
            {
                vtp.addTab(""+i, new JPanel());
            }
            vtp.setSelectedIndex(0);

            getContentPane().add(vtp);
            pack();
            setVisible(true);
        }
    }
import javax.swing.JFrame;
导入javax.swing.JPanel;
公共类TabbedPaneeExample扩展了JFrame
{
公共静态void main(字符串[]args)
{
TabbedPaneExample example=new TabbedPaneExample();
例如:go();
}
公开作废go()
{
ValidatingAbbedPane vtp=新的ValidatingAbbedPane();
对于(int i=0;i=0&&newIndex

我在中留下了注释,以显示我在实际程序中验证的位置。

好的,决定继续我自己的示例;两个类,运行main类会生成一个swing窗口,允许您每隔单击一次就更改选项卡

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class TabbedPaneExample extends JFrame
    {
        public static void main(String[] args)
        {
            TabbedPaneExample example = new TabbedPaneExample();
            example.go();
        }

        public void go()
        {
            ValidatingTabbedPane vtp = new ValidatingTabbedPane();
            for(int i=0; i<3; i++)
            {
                vtp.addTab(""+i, new JPanel());
            }
            vtp.setSelectedIndex(0);

            getContentPane().add(vtp);
            pack();
            setVisible(true);
        }
    }
import javax.swing.JFrame;
导入javax.swing.JPanel;
公共类TabbedPaneeExample扩展了JFrame
{
公共静态void main(字符串[]args)
{
TabbedPaneExample example=new TabbedPaneExample();
例如:go();
}
公开作废go()
{
ValidatingAbbedPane vtp=新的ValidatingAbbedPane();
对于(int i=0;i=0&&newIndex

我在中留下了注释,以显示我在实际程序中验证的位置。

@rcook-我已将您的setSelected()方法插入到我的原始代码中。 运行程序时,将显示蓝色面板(在“颜色”选项卡下)

如果你去二月的小组(在任务单下)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class VerifyBeforeLeavingTab extends JFrame
{
    private static final long       serialVersionUID    = 1L;
    private static final JCheckBox  chckBoxAllowToLeave = new JCheckBox("Allow to leave");

    public VerifyBeforeLeavingTab()
    {
        setSize(700, 700);

        JTabbedPane mainTabbedPane = new JTabbedPane(JTabbedPane.TOP);

        MyJTabbedPane colorsPane = new MyJTabbedPane(JTabbedPane.TOP, "colors");
        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.blue);
        colorsPane.addTab("Blue", bluePanel);

        JPanel redPanel = new JPanel();
        redPanel.setBackground(Color.red);
        colorsPane.addTab("Red", redPanel);

        mainTabbedPane.addTab("Colors", colorsPane);

        JTabbedPane monthsPane = new MyJTabbedPane(JTabbedPane.TOP, "months");

        JPanel janPanel = new JPanel();
        monthsPane.addTab("January", janPanel);

        JPanel febPanel = new MyUnleavableJPanel();
        monthsPane.addTab("February", febPanel);

        mainTabbedPane.addTab("Months", monthsPane);

        add(mainTabbedPane, BorderLayout.CENTER);

        getContentPane().add(mainTabbedPane);
    }

    private class MyUnleavableJPanel extends JPanel
    {
        private static final long   serialVersionUID    = 1L;

        public MyUnleavableJPanel()
        {
            // final JCheckBox chckBoxAllowToLeave = new JCheckBox("Allow to leave");
            chckBoxAllowToLeave.setBounds(100, 100, 50, 50);
            chckBoxAllowToLeave.setSelected(true);
            this.add(chckBoxAllowToLeave);

            addHierarchyListener(new HierarchyListener()
            {
                public void hierarchyChanged(HierarchyEvent e)
                {
                    if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) != 0)
                    {
                        if (isShowing())
                        {
                            System.out.println("Showing an unleavable panel");
                        }
                        else
                        {
                            // TODO: Do not let them leave if this JCheckbox is selected
                            if (chckBoxAllowToLeave.isSelected())
                            {
                                System.out.println("OK to leave");
                            }
                            else
                            {
                                System.out.println("Not allowed to leave");
                            }
                        }
                    }
                }
            });
        }
    }

    private class MyJTabbedPane extends JTabbedPane
    {
        private static final long   serialVersionUID    = 1L;

        public MyJTabbedPane(int i, String name)
        {
            super(i);
            setName(name);
        }

        /**
         * override of selecting a new panel; the new panel selection is only allowed after the current panel determines
         * that its data is valid.
         */
        @Override
        public void setSelectedIndex(int newIndex)
        {
            System.out.println("Now on '" + getName() + "' tab #" + newIndex);
            int currentIndex = getSelectedIndex();
            if (newIndex >= 0 && newIndex < getTabCount())
            {
                // if we are currently setting the selected tab for the first
                // time, we don't need to validate the currently selected panel;
                // same if we're (somehow) selecting the current panel
                if (currentIndex == -1)
                {
                    super.setSelectedIndex(newIndex);
                }
                else
                {
                    if (currentIndex != newIndex)
                    {
                        if (chckBoxAllowToLeave.isSelected())
                        {
                            super.setSelectedIndex(newIndex);
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Not this time");
                        }

                    }
                }
            }
            else
            {
                System.out.println("setting new tabbed pane index to " + newIndex + "; wonder why?");
            }
        }
    }

    private static void createAndShowGUI()
    {
        // Create and set up the window.
        VerifyBeforeLeavingTab frame = new VerifyBeforeLeavingTab();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    createAndShowGUI();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    System.exit(0);
                }
            }
        });
    }

}