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

SpringLayout should take care of "Implicit" constraints between components.

XMLWordPrintable

      Name: rmT116609 Date: 08/22/2004


      A DESCRIPTION OF THE REQUEST :
      The current SpringLayout implements only allow developers to specify the "relative position" of one component of another by setting a constraint, while it doesn't know the "reverse meaning" of the constraint. This produces a rather surprising behavior.

      Try the following code and you see the problem.

      As you can see, there are four components, laying out from left to right:
      (1) JLabel
      (2) JTextField
      (3) two JButtons

      If we change the first constraint setting line
      from
            layout.putConstraint(SpringLayout.WEST, inputLabel, 10,
              SpringLayout.WEST, this);

      to
           layout.putConstraint(SpringLayout.WEST, this, 10,
             SpringLayout.WEST, inputLabel);

       And this behavior is rather counter intuitive because the two statements are "conceptually" the same thing. When there are a lot of components on the screen, it would be very difficult to resolve such situation because the relations are simply too complex to be resolved.

      I wonder if the SpringLayout implement could produce the "implicit constraint" internally, the code would be much cleaner and easier to write.


      JUSTIFICATION :
      Reduce the amount of error-prone constraint setting code. It is very hard to make things laid out correctly if there are a lot of components within a single container.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      chaning the first constraint line
      from
            layout.putConstraint(SpringLayout.WEST, inputLabel, 10,
              SpringLayout.WEST, this);

      to
           layout.putConstraint(SpringLayout.WEST, this, 10,
             SpringLayout.WEST, inputLabel);

      means the same thing...
      ACTUAL -
      chaning the first constraint setting line
      from
            layout.putConstraint(SpringLayout.WEST, inputLabel, 10,
              SpringLayout.WEST, this);

      to
           layout.putConstraint(SpringLayout.WEST, this, 10,
             SpringLayout.WEST, inputLabel);

      ---------- BEGIN SOURCE ----------
            SpringLayout layout=(SpringLayout) super.getLayout();
            inputLabel=new JLabel();
            inputField=new JTextField();
            submitButton=new JButton();
            clearButton=new JButton();
            // call this to set the localized labels
            refreshLabels();
            // add the components.
            add(inputLabel);
            add(inputField);
            add(submitButton);
            add(clearButton);
            
            // set the spring constraints.
            layout.putConstraint(SpringLayout.WEST, inputLabel, 10,
              SpringLayout.WEST, this);
            layout.putConstraint(SpringLayout.WEST, inputField, 5,
              SpringLayout.EAST, inputLabel);
              
            layout.putConstraint(SpringLayout.WEST, submitButton, 10,
              SpringLayout.EAST, inputField);
            layout.putConstraint(SpringLayout.WEST, clearButton, 5,
              SpringLayout.EAST, submitButton );
            layout.putConstraint(SpringLayout.EAST, this, 10,
              SpringLayout.EAST, clearButton);

      ---------- END SOURCE ----------


      (Incident Review ID: 232691)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: