PRB: BorderLayout Does Not Accept Null Constraints

ID: Q184599

The information in this article applies to:

SYMPTOMS

When adding a component using a null constraint the following error message is appears:

   BorderLayout: cannot add to layout: unknown constraint: null

This occurs when using a BorderLayout manager and calling the add(Component) method without passing a constraint.

NOTE: The error message is displayed but the application continues to run.

CAUSE

In the JDK 1.02 model, BorderLayout allowed you to add components with null constraints and then resize the components later. In the JDK 1.1 model, if you use a null constraint, it is treated as "Center" and the previously added "Center" component will be removed from the container. Implementing the JDK 1.1 model breaks existing JDK 1.02 applications that rely on this behavior. Microsoft's implementation of BorderLayout follows the JDK 1.02 implementation.

RESOLUTION

If you are writing a JDK 1.1 application, pass a constraint of "Center" instead of using a null constraint. For example the statement

   panel.add(myComponent);

would become

   panel.add(myComponent,"Center");

STATUS

This behavior is by design.

MORE INFORMATION

The sample code below demonstrates a typical JDK 1.02 application. To avoid the error messages when running the program below, set the layout manager to 'null' instead of the default BorderLayout manger.

Sample Code

   import java.awt.*;

   class AddNull extends Frame {
     public static void main(String args[])
     {
       Frame frame=new AddNull("AWT Layout");
       frame.resize(320,200);
       frame.show();
     }
     public AddNull(String title) {
       super(title);
       Button okay=new Button("OK");
       Button cancel=new Button("Cancel");
       Label label=new Label("Demo done.");
   //  setLayout(null);  // uncomment to use a 'null' layout manager.
       add(label);
       add(okay);
       add(cancel);
       label.reshape(120,80,100,30);
       okay.reshape(30,150,80,30);
       cancel.reshape(180,150,80,30);
     }
     public boolean action(Event e, Object o) {
       hide();
       dispose();
       System.exit(0);
       return true;
     }
   }

Steps to Reproduce Behavior

1. Save the sample code to a file named AddNull.java.

2. Compile the sample code.

3. Run the class using this command:

      jview AddNull

4. Notice the error messages on the console.

5. Repeat steps 1-3 after removing the comment markers from the

   setLayout(null) instruction.

6. Notice the error messages do not appear on the console.

Additional query words: awt null constraint layout

Keywords          : kbcode AWTPkg 
Version           : WINDOWS:
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: April 29, 1998