JDK-4530310 : call to Container.add(String, Component) leads to unexpected IAE
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: solaris_2.5
  • CPU: sparc
  • Submitted: 2001-11-21
  • Updated: 2010-11-15
  • Resolved: 2010-11-15
Related Reports
Relates :  
Description
Name: idR10193			Date: 11/21/2001


Please see the sample below that shows how call to
"Component add(String name, Component comp)" method of Container class
leads to unexpected IllegalArgumenException.

Sample code:
============add.java (Sample1)===============================
import java.awt.*;

public class add {

    public static void main(String argv[]) {
        GridBagLayout gbl = new GridBagLayout();
        Container parent = new Container();
        parent.setLayout(gbl);

        Component comp = new Component(){};

        parent.add("component", comp);
    }
}
============ end of add.java =========================
javac add.java
~/bugs
java add
Exception in thread "main" java.lang.IllegalArgumentException:
cannot add to layout: constraints must be a GridBagConstraint
        at java.awt.GridBagLayout.addLayoutComponent(GridBagLayout.java:605)
        at java.awt.Container.addImpl(Container.java:485)
        at java.awt.Container.add(Container.java:313)
        at add.main(add.java:12)
~/bugs

======================================================================

Comments
EVALUATION This method is obsolete, which is probably why the documentation has been removed. The code has not been modified in more than five years. We could update the javadoc to explain that the "name" field must be a layout constraint, not just any name. ###@###.### 2001-11-21
21-11-2001