United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4286825 : Adding Same component to JTabbedPane throws ArrayIndexOutOfBoundsException

Details
Type:
Bug
Submit Date:
1999-11-01
Status:
Resolved
Updated Date:
2001-02-28
Project Name:
JDK
Resolved Date:
2001-02-28
Component:
client-libs
OS:
solaris_2.6,windows_nt,linux
Sub-Component:
javax.swing
CPU:
x86,sparc
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.2.2,1.3.0
Fixed Versions:
1.4.0 (beta)

Related Reports
Relates:
Relates:

Sub Tasks

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
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
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
WORK AROUND

None
                                     
2004-06-11
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
merlin-beta

FIXED IN:
merlin-beta

INTEGRATED IN:
merlin-beta


                                     
2004-06-14



Hardware and Software, Engineered to Work Together