Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4687196

Container.add(Component, int) throws unspecified IllegalArgmentException

XMLWordPrintable

    • sparc
    • solaris_7

      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.

            dav Andrei Dmitriev (Inactive)
            xwangsunw Xiaozhong Wang (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: