JDK-4286825 : Adding Same component to JTabbedPane throws ArrayIndexOutOfBoundsException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.2,1.3.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux,solaris_2.6,windows_nt
  • CPU: x86,sparc
  • Submitted: 1999-11-01
  • Updated: 2001-02-28
  • Resolved: 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 Availability Release.

To download the current JDK release, click here.
1.4.0 betaFixed
Related Reports
Relates :  
Relates :  
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(). 


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);


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

    // 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");
    else if (evt.getSource() == fail) {
      System.out.println("Test Failed");
    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);
    return b;

  public static void main(String[] args) {
    AddSameComp t = new AddSameComp();

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


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); }

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