title: How to prevent leaving a Tab on JTabbedPane
body: <p>I have an application that has nested JTabbedPanes.  Each of the JTabbedPanes include other JTabbedPanes.</p>
  
  <p>I want to check something before allowing the user to leave the current tab.</p>
  
  <p>When you run the code below, you'll see that the Months->February tab has an ALLOW checkbox.</p>
  
  <p>If the ALLOW checkbox is selected, then I want to allow the user navigate to leave the current panel.  If it is not, then they can't leave until it is.</p>
  
  <p>I can't seem to find a way to handle the request to leave BEFORE the next Component (which may be on another JTabbedPane) is shown.</p>
  
  <p>This can be demonstrated by going to the Months->February tab, and then selecting the Colors Tab.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>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 &amp; 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);
                  }
              }
          });
      }
  
  }
  </code></pre>