United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4687196 : Container.add(Component, int) throws unspecified IllegalArgmentException

Details
Type:
Bug
Submit Date:
2002-05-17
Status:
Closed
Updated Date:
2006-07-17
Project Name:
JDK
Resolved Date:
2006-07-17
Component:
client-libs
OS:
solaris_7
Sub-Component:
java.awt
CPU:
sparc
Priority:
P4
Resolution:
Duplicate
Affected Versions:
1.4.1
Fixed Versions:
7

Related Reports
Duplicate:
Relates:
Relates:

Sub Tasks

Description
If components are added to Container using Container.add(Component comp, int
index), and index is not consecutive, IllegalArgumentException will be thrown.
This behavior needs to be documented.

Here is an example:

import java.awt.*;

public class ContainerAdd {
    public static void main(String[] args) throws Exception {
        Container c = new Container();
        int[]  values = {0, 1, 2, 3, 4, 8}; 
        LWComponent comp = new LWComponent();  
        for(int i=0;i<values.length;i++) {
            try {
                c.add(new LWComponent(), values[i]);
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Unexpected ArrayIndexOutOfBoundsException, index=" + values[i]);
            } catch (IllegalArgumentException e) {
                System.out.println("IllegalArgumentExeption is thrown for index " + values[i]);
                e.printStackTrace();
            }
        }
    }
}

class LWComponent extends Component {
    public LWComponent() {
    }
}

Output in build 1.4.1-beta-b11 is:

IllegalArgumentExeption is thrown for index 8
java.lang.IllegalArgumentException: illegal component position
        at java.awt.Container.addImpl(Container.java:605)
        at java.awt.Container.add(Container.java:346)
        at ContainerAdd.main(ContainerAdd.java:10)

Another unspecified behavior is that if the same instance of Component
is added repeatly to the container, IllegalArgumentException is also
thrown from the second time this component is added to the Container.

                                    

Comments
PUBLIC COMMENTS

.
                                     
2004-06-10
EVALUATION

Will fix for tiger.

Reviewed by Brent.  CCC approved.  Fixed for tiger.
###@###.### 2003-09-12

The second issue from the description doesn't reproducible: adding follwong lines into ContainerAdd has no effect:

        LWComponent comp1 = new LWComponent();  
        LWComponent comp2 = new LWComponent();  
        LWComponent comp3 = new LWComponent();  
        LWComponent comp4 = new LWComponent();  
        c.add(comp1);
        c.add(comp2);
        c.add(comp3);
        c.add(comp4);

Asked submitter for additional info.
###@###.### 2005-05-14 14:40:58 GMT
                                     
2005-05-14
SUGGESTED FIX

*** /tmp/geta13085	Tue May 24 14:20:50 2005
--- Container.java	Tue May 24 14:20:20 2005
***************
*** 972,982 ****
       * @param     index the position in the container's list at which to
       *                 insert the component, where <code>-1</code> 
       *                 means append to the end
!      * @exception IllegalArgumentException if <code>index</code> is invalid
       * @exception IllegalArgumentException if adding the container's parent
       *			to itself
       * @exception IllegalArgumentException if adding a window to a container
--- 972,980 ----
       * @param     index the position in the container's list at which to
       *                 insert the component, where <code>-1</code> 
       *                 means append to the end
!      * @exception IllegalArgumentException if <code>index</code> is not consecutive with previous values
       * @exception IllegalArgumentException if adding the container's parent
       *			to itself
       * @exception IllegalArgumentException if adding a window to a container

###@###.### 2005-05-24 10:23:54 GMT
                                     
2005-05-14
EVALUATION

There seem two issues with the specification:
 - We have to describe what "incorrect index" means there.
	    if (index > ncomponents || (index < 0 && index != -1)) {
		throw new IllegalArgumentException(
			  "illegal component position");
Suppose it's the same as "illegal component position" and there is nothing about "incorrect index" in the source.
 - Thrown "illegal component position" by the test in description.
 It should be clarified in terms of ncomponents and zero/negative values.
                                     
2006-05-17



Hardware and Software, Engineered to Work Together