JDK-4286825 : Adding Same component to JTabbedPane throws ArrayIndexOutOfBoundsException
  • Type: Bug
  • Status: Resolved
  • Resolution: Fixed
  • Component: client-libs
  • Sub-Component: javax.swing
  • Priority: P4
  • Affected Version: 1.2.2,1.3.0
  • OS: linux,solaris_2.6,windows_nt
  • CPU: x86,sparc
  • Submit Date: 1999-11-01
  • Updated Date: 2001-02-28
  • Resolved Date: 2001-02-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
Other
1.4.0 betaResolved
Related Reports
Relates :  
Relates :  
Description
If a user adds a component that already exists in JTabbedPane will throw a ArrayIndexOutOfBoundsException. The problems seems that we get an invalid index
after deletion. We need to insert current index of tabbedpane not the pages.size(). 

TestCode:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.accessibility.*;

public class AddSameComp extends JFrame implements ActionListener{
  String path;
  Container contentPane = getContentPane();
  JTabbedPane jtp = new JTabbedPane();
  JButton b1 = new JButton("button in panel 1");
  JButton b2 = new JButton("button in panel 2");
  JPanel panelOne = new JPanel();
  JPanel panelTwo = new JPanel();  
  JPanel panelThree = new JPanel();
  JLabel newTab = new JLabel("This is a New Tab Added");
  String textOut;
  

  private JPanel buttonPanel = new JPanel();
  private JPanel bottomPanel = new JPanel(new BorderLayout());
  private JPanel statusPanel = new JPanel();
  Object o;
  
  private JLabel label = new JLabel("Press Blue \"Add Component one again\" button to start test.");
  private JButton start;
  private JButton pass;
  private JButton fail;

  AddSameComp() {
    super("AddSameComp Test");
    
    // Add Invoke Buttons
    
    start = addButton(buttonPanel,"Add Component one again",Color.blue);
    pass = addButton(buttonPanel,"Pass",Color.green);
    fail = addButton(buttonPanel,"Fail",Color.red);
    bottomPanel.add(buttonPanel,BorderLayout.NORTH);
    statusPanel.add(label);
    bottomPanel.add(statusPanel,BorderLayout.SOUTH);

    contentPane.add(bottomPanel,BorderLayout.SOUTH);

    
    // Add Tab Panes Here!!!
    jtp.add("Panel One",panelOne);
    jtp.add("Panel Two",panelTwo);

    contentPane.add(jtp,BorderLayout.CENTER);
 
    // Add Window handler
    addWindowListener(new WindowAdapter(){ 
      public void windowClosing(WindowEvent event){ System.exit(0);}
    });
  }

   public void actionPerformed(ActionEvent evt) {
    String cmd = evt.getActionCommand();
    if (evt.getSource() == pass) {
      System.out.println("Test Passed");
      System.exit(0);
    }
    else if (evt.getSource() == fail) {
      System.out.println("Test Failed");
      System.exit(1);
    }
    else if (evt.getSource() == start) {
      try {
	jtp.add("Panel One",panelOne);
	label.setText("T est passed! ArrayIndexOutOfBoundsException was not thrown!!");
      }
      catch (ArrayIndexOutOfBoundsException e) {
	label.setText("Test Failed! ArrayIndexOutOfBoundsException was thrown!!");
      }
    }
  }

  public JButton addButton(JPanel addComponent, String name, Color color) {
    JButton b = new JButton(name);
    b.setBackground(color);
    b.addActionListener(this);
    addComponent.add(b);
    return b;
  }

  public static void main(String[] args) {
    
    AddSameComp t = new AddSameComp();
    t.setSize(450,300);
    t.setVisible(true);
  }    
}

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: merlin-beta FIXED IN: merlin-beta INTEGRATED IN: merlin-beta
2004-06-14

WORK AROUND None
2004-06-11

SUGGESTED FIX In the insertTab(String,.....) code for JTabbedPane.java we set int i; change to int i = -1; After code: if (component != null && (i = indexOfComponent(component)) != -1) { removeTabAt(i); } if (i!=-1) { pages.insertElementAt(new Page(this, title != null? title : "", icon, disabledIcon, component, tip), i); } else { pages.insertElementAt(new Page(this, title != null? title : "", icon, disabledIcon, component, tip), index); }
2004-06-11

EVALUATION This is true. We remove the tab corresponding to the component if it already exists before we create the new tab/Page, which means that the index for which the new tab is added is one too large. To fix this, we can check for the condition and adjust the new tab index accordingly. amy.fowler@Eng 2001-02-16
2001-02-16