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

Suggestion for GridBagPanel to simplify GridBagLayout/GridBagConstraints

XMLWordPrintable

    • 1.1
    • sparc
    • solaris_2.5
    • Not verified

      This is a suggestion for a small new class, GridBagPanel, that
      simplifies some of the drudgery of using GridBagLayout and
      GridBagConstraints.

      If you follow the guidelines in GridBagLayout.java, you'll make a
      Panel, set its layout, and then for every component, you have to both
      set its constraints and add it to the panel. The guidelines suggest an
      auxiliary routine to simplify this, but even that gets to be a pain,
      every time you use GridBagLayout.

      The attached class, GridBagPanel is a Panel that has GridBagLayout set
      by default, it creates a set of GridBagConstraints for you that you can
      easily access, and it overrides the standard add method so that it
      additionally registers the component being added with the panel's
      layout, using the current contraints.

      Using it, I found my test program got shorter and simpler in its GUI
      section, where GridBagLayout was concerned.

      Here is the class itself. Note that no changes to any other classes or
      interfaces are required, and it does not affect any existing code using
      GridBagLayout and GridBagConstraints.

      ------------------------------------
      class GridBagPanel extends Panel
      {
        public GridBagPanel() {
          setLayout(new GridBagLayout());
        }

        public Component add(Component comp) {
          LayoutManager lm = getLayout();
          if (lm instanceof GridBagLayout) {
            ((GridBagLayout)lm).setConstraints(comp, currentConstraints);
          }
          return super.add(comp);
        }

        public GridBagConstraints currentConstraints = new GridBagConstraints();
      }
      ------------------------------------


      If you're interested, here is a small test program that uses GridBagPanel,
      and in fact, uses nested ones....
      ------------------------------------
      import java.awt.*;

      public class Main extends Frame
      {
        public static void main(String[] args) {
          new Main().show();
        }

        Main() {
          GridBagPanel p = new GridBagPanel();

          p.currentConstraints.gridwidth = GridBagConstraints.REMAINDER;
          p.currentConstraints.weightx = 1.0;
          p.currentConstraints.weighty = 1.0;
          p.currentConstraints.fill = GridBagConstraints.HORIZONTAL;
          
          Component c1 = new DemoCanvas(100, 50, Color.red);
          p.add(c1);
          
          GridBagPanel c2 = new GridBagPanel();

          Component c21 = new DemoCanvas(100, 50, Color.green);
          c2.currentConstraints.gridwidth = 1;
          c2.add(c21);

          Component c22 = new DemoCanvas(100, 50, Color.green);
          c2.currentConstraints.gridwidth = GridBagConstraints.REMAINDER;
          c2.currentConstraints.weightx = 1.0;
          c2.currentConstraints.fill = GridBagConstraints.HORIZONTAL;
          c2.add(c22);

          p.add(c2);

          Component c31 = new DemoCanvas(100, 50, Color.blue);
          p.currentConstraints.gridwidth = 1;
          p.add(c31);
          
          Component c32= new DemoCanvas(100, 50, Color.blue);
          p.add(c32);
          
          Component c33= new DemoCanvas(100, 50, Color.blue);
          p.add(c33);

          add("Center", p);
          pack();
        }

      }


      class DemoCanvas extends Canvas
      {
        DemoCanvas(int w, int h, Color c) {

          this.c = c;
          d = new Dimension(w, h);
        }

        public void paint(Graphics g) {
          Dimension s = size();
          g.setColor(c);
          g.drawLine(0, 0, s.width, s.height);
          g.drawLine(s.width, 0, 0, s.height);
          g.drawRect(0, 0, s.width-1, s.height-1);
        }

        public Dimension minimumSize() { return d; }
        public Dimension preferredSize() { return d; }

        Color c;
        Dimension d;
      }

            duke J. Duke
            jjg Jonathan Gibbons
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: