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

Container.addImpl() does not always call LayoutManager.addLayoutComponent()

XMLWordPrintable

    • b95
    • x86
    • windows_xp

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      import java.awt.*;

      /** NoisyFlowLayout.java

      Demonstrates a bug in Container.addImpl() in which
      LayoutManagers which do not implement LayoutManager2
      are not notified when a Component is added unless both
      a contstraint/name is provided and it is not null.

      Expected output:
        adding java.awt.Label[...,text=1] at null
        adding java.awt.Label[...,text=2] at non-null
        adding java.awt.Label[...,text=3] at null
        adding java.awt.Label[...,text=4] at non-null
        adding java.awt.Label[...,text=5] at null

      Actual output:
        adding java.awt.Label[...,text=2] at non-null
        adding java.awt.Label[...,text=4] at non-null

      The problem is that Container.addImpl() does this:

        if (layoutMgr != null) {
          if (layoutMgr instanceof LayoutManager2) {
              ((LayoutManager2)layoutMgr).addLayoutComponent(comp, constraints);
          } else if (constraints instanceof String) {
              layoutMgr.addLayoutComponent((String)constraints, comp);
          }
        }

      when it should be doing this:

        if (layoutMgr != null) {
          if (layoutMgr instanceof LayoutManager2) {
              ((LayoutManager2)layoutMgr).addLayoutComponent(comp, constraints);
          } else {
              layoutMgr.addLayoutComponent((constraints instanceof String)?(String)constraints:null, comp);
          }
        }

      I think this is a bug. But if somehow this is declared "not a bug"
      then at the very least this behaviour should be documented. (You
      will definitely have to mention that while a LayoutManager2 will
      have addLayoutComponent() called every time a component is added
      to the Container, that a plain LayoutManager will only have
      addLayoutComponent() called when a non-null constraint is provided.)

      Also see bug#4413418 which indirectly reported this problem, but
      then got it wrong in the Evaluation and dismissed it.
      */
      public class NoisyFlowLayout extends FlowLayout {

        public void addLayoutComponent(String name, Component comp) {
          System.out.println("adding " + comp + " at " + name);
          super.addLayoutComponent(name, comp);
        }

        public static void main(String[] argv) {
          String constraint = "non-null", nullConstraint = null;
          Panel p = new Panel(new NoisyFlowLayout());
          p.add(new Label("1"));
          p.add(new Label("2"), constraint);
          p.add(new Label("3"), nullConstraint);
          p.add(constraint, new Label("4"));
          p.add(nullConstraint, new Label("5"));
        }
      }



      REPRODUCIBILITY :
      This bug can be reproduced always.

            son Oleg Sukhodolsky (Inactive)
            dav Andrei Dmitriev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: