title: Java - Adding tab to JTabbedPane causing StackOverflowError (Disabling a firing ChangeEvent?)
body: <p>Hey StackOverflow community, I'm having a StackOverflow problem.</p>
  
  <p>I'm having difficulty adding a new tab to my GUI's JTabbedPane container, when the [+] tab is selected. So far, whenever I click the [+] tab, new tabs are appended until a StackOverflowError occurs. </p>
  
  <p>A new tab is added to the JTabbedPane when the following condition is true.</p>
  
  <pre><code>if(songPanel.getSelectedIndex()==songPanel.getTabCount()-1){
  ...
  }
  </code></pre>
  
  <p>I've tried to revert back to the previously selected tab to avoid tabs being added repeatedly to the JTabbedPane, to no avail. When the ChangeEvent actuator is fired, does it stay on indefinitely? I haven't come across anything useful in the SE7 API.</p>
  
  <blockquote>
    <p>Relevant code (non-compilable, excerpt from larger program. May be missing brackets, only because I copy-pasted excerpts of the code, and liable to make mistakes)</p>
  </blockquote>
  
  <pre><code>@Override
      public void init(){
          setLayout(new GridLayout(MAIN_LAYOUT_ROWS, MAIN_LAYOUT_COLUMNS));
          add(renderPanel = new JScrollPane());
          add(controlPanel = new JPanel());
          add(colourPanel = new JPanel());
          add(songPanel = new JTabbedPane());
  
          //songPanel options
          songPanel = new JTabbedPane();
          songPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
          songPanel.addTab("#1", new JTextArea());
          songPanel.addTab("+", null, new JLabel(), "+");
  
          Container cp = getContentPane();
          cp.add(BorderLayout.SOUTH, songPanel);
  
          //integrate songPanel changeListener 
          songPanel.addChangeListener(new ChangeListener(){
  
              @Override //Method called when selected tab changes
              public void stateChanged(ChangeEvent e){
                  try {          
                      if(songPanel.getSelectedIndex()==songPanel.getTabCount()-1){
                          addTab("songPanel");
                      }
                  } catch (StackOverflowError soe){soe.printStackTrace();}
              }
          });
  //*************************************************************************
      @Override
      public void start(){
  
      }
  //*************************************************************************
      private void addTab(String panelName){
          System.out.println("ADDING TAB");
          if(panelName.equals("songPanel")){
              String tabName = ("#" + Integer.toString(songPanel.getTabCount()-1));
              songPanel.insertTab(tabName, null, new JTextField(), tabName,           songPanel.getTabCount()-2);
          }
      }
  }
  //**************************************************************************
  }
  </code></pre>
  
  <p>I've tried:</p>
  
  <ul>
  <li>Setting a revert index in the addTab() method, so the newest tab is selected (still results in StackOverflowError)</li>
  </ul>
  
  <p>Note this line:</p>
  
  <pre><code>songPanel.getSelectedIndex()==songPanel.getTabCount()-1)
  </code></pre>
  
  <blockquote>
    <p>Both "songPanel.getSelectedIndex()" and "songPanel.getTabCount()-1)" are always equal, so condition is always true (<strong>causing the StackOverflowError</strong>)</p>
    
    <p>Error message:</p>
  </blockquote>
  
  <pre><code>java.lang.StackOverflowError
  at javax.swing.text.StyleContext$SmallAttributeSet.getAttributeNames(StyleContext.java:947)
  at javax.swing.text.StyleContext$SmallAttributeSet.containsAttributes(StyleContext.java:973)
  at javax.swing.text.StyleContext$SmallAttributeSet.equals(StyleContext.java:852)
  at java.util.WeakHashMap.eq(WeakHashMap.java:282)
  at java.util.WeakHashMap.get(WeakHashMap.java:379)
  at java.util.Collections$SynchronizedMap.get(Collections.java:2031)
  at javax.swing.text.StyleContext.getImmutableUniqueSet(StyleContext.java:520)
  at javax.swing.text.StyleContext.addAttributes(StyleContext.java:340)
  at javax.swing.text.AbstractDocument$AbstractElement.addAttributes(AbstractDocument.java:1985)
  at javax.swing.text.AbstractDocument$AbstractElement.&lt;init&gt;(AbstractDocument.java:1777)
  at javax.swing.text.AbstractDocument$LeafElement.&lt;init&gt;(AbstractDocument.java:2502)
  at javax.swing.text.AbstractDocument$BidiElement.&lt;init&gt;(AbstractDocument.java:2674)
  at javax.swing.text.AbstractDocument.&lt;init&gt;(AbstractDocument.java:149)
  at javax.swing.text.AbstractDocument.&lt;init&gt;(AbstractDocument.java:109)
  at javax.swing.text.PlainDocument.&lt;init&gt;(PlainDocument.java:90)
  at javax.swing.text.PlainDocument.&lt;init&gt;(PlainDocument.java:80)
  at javax.swing.text.DefaultEditorKit.createDefaultDocument(DefaultEditorKit.java:130)
  at javax.swing.plaf.basic.BasicTextUI.installUI(BasicTextUI.java:799)
  at javax.swing.JComponent.setUI(JComponent.java:655)
  at javax.swing.text.JTextComponent.setUI(JTextComponent.java:338)
  at javax.swing.text.JTextComponent.updateUI(JTextComponent.java:348)
  at javax.swing.text.JTextComponent.&lt;init&gt;(JTextComponent.java:322)
  at javax.swing.JTextField.&lt;init&gt;(JTextField.java:231)
  at javax.swing.JTextField.&lt;init&gt;(JTextField.java:172)
  at application.Analyzer.addTab(Analyzer.java:133)
  at application.Analyzer.access$100(Analyzer.java:24)
  at application.Analyzer$1.stateChanged(Analyzer.java:101)
  at javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:416)
  at javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:270)
  at javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:132)
  at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:67)
  at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabbedPane.java:616)
  at javax.swing.JTabbedPane.insertTab(JTabbedPane.java:735)
  at application.Analyzer.addTab(Analyzer.java:133)
  at application.Analyzer.access$100(Analyzer.java:24)
  .
  .
  .
  </code></pre>
  
  <p>Do you have any suggestions? I know it's kind of vague, but I'm really not sure what is going wrong. </p>
  
  <p>Any suggestions? 
  Thanks. 
  Tyler</p>