JDK-4687196 : Container.add(Component, int) throws unspecified IllegalArgmentException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_7
  • CPU: sparc
  • Submitted: 2002-05-17
  • Updated: 2017-05-16
  • Resolved: 2006-07-17
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.
JDK 7
7Resolved
Related Reports
Duplicate :  
Relates :  
Relates :  
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
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

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

PUBLIC COMMENTS .
2004-06-10