The Source for Java Technology Collaboration


Home | Changes | Index | Search | Go

ExplicitTableBuilderExample

This is ExplicitTableBuilder example for John O'Conner's Layout Manager Showdown.

AddressBookDemo.png

Webstart : AddressBookDemo.jnlp

package org.pietschy.explicit.demo;

import org.pietschy.explicit.DluSupport;
import org.pietschy.explicit.NestedBuilder;
import org.pietschy.explicit.TableBuilder;
import org.pietschy.explicit.style.LayoutStyle;

import javax.swing.*;

public class AddressBookDemo
extends JPanel
//extends DebugPanel // uncomment for debug mode.
{
   public AddressBookDemo()
   {
      TableBuilder mainBuilder = new TableBuilder(this);
      
      // add the list so it fills and spans two rows.
      mainBuilder.add(createList(), 0, 0, 2, 1).fill();

      // Add the form using a nested builder that fills its parent cell.
      NestedBuilder form = mainBuilder.createNestedBuilder(0, 1);
      form.fill();

      int row = 0;
      form.add(createLabel("Last Name"), row, 0);
      form.add(createTextField("Martian"), row, 1).fillX();
      form.add(createLabel("First Name"), row, 2, 1, 2);
      form.add(createTextField("Martian"), row, 4).fillX();

      row++;
      form.add(createLabel("Phone"), row, 0);
      form.add(createTextField("805-876-5432"), row, 1).fillX();
      form.add(createLabel("Email"), row, 2);
      form.add(createTextField("marvin@wb.com"), row, 3, 1, 2).fillX();

      row++;
      form.add(createLabel("Address 1"), row, 0);
      form.add(createTextField("1001001010101 Martian Way"), row, 1, 1, 4).fillX();

      row++;
      form.add(createLabel("Address 2"), row, 0);
      form.add(createTextField("Suite 10111011"), row, 1, 1, 4).fillX();

      row++;
      form.add(createLabel("City"), row, 0);
      form.add(createTextField("Ventura"), row, 1).fillX();

      row++;
      form.add(createLabel("State"), row, 0);
      form.add(createTextField("CA"), row, 1).fillX();
      form.add(createLabel("Postal Code"), row, 2, 1, 2);
      form.add(createTextField("93001"), row, 4).fillX();

      row++;
      form.add(createLabel("Country"), row, 0);
      form.add(createTextField(""), row, 1).fillX();

      // the first label column is right aligned.
      form.column(0).alignRight();
      // make the value columns grow equally to fit any extra space.
      form.column(1).grow(0.5);
      form.column(4).grow(0.5);

      // Add the buttons to the second row using a nested builder.
      NestedBuilder buttons = mainBuilder.createNestedBuilder(1, 1);
      buttons.alignTop();

      buttons.add(createButton("New"), 0, 0);
      buttons.add(createButton("Delete"), 0, 1);
      buttons.add(createButton("Edit"), 0, 2);
      buttons.add(createButton("Save"), 0, 3);
      buttons.add(createButton("Cancel"), 0, 4);


      // make the form and button column grow to fit.
      mainBuilder.column(1).grow(1);
      // make the last (button) row grow to fit.
      mainBuilder.row(1).grow(1);

      LayoutStyle style = mainBuilder.layoutStyle();
      // add the standard column gap between the list and the form.
      mainBuilder.column(1).paddingLeft(style.unrelatedColumnGap());
      // add the standard row gap between the form and the buttons.
      mainBuilder.row(1).paddingTop(style.unrelatedRowGap());


      // apply the layout.
      mainBuilder.buildLayout();

   }

   private JLabel createLabel(String text)
   {
      return new JLabel(text);
   }

   private JTextField createTextField(String text)
   {
      return new JTextField(text, 10);
   }


   private JButton createButton(String text)
   {
      return new JButton(text);
   }

   private JScrollPane createList()
   {
      JList characters = new JList(new String[]
      {
         "Bunny, Bugs",
         "Cat, Sylvester",
         "Coyote, Wile E.",
         "Devil, Tasmanian",
         "Duck, Daffy",
         "Fud, Elmer",
         "Le Pew, Pep\u00E9",
         "Martian, Marvin",
      });

      JScrollPane scrollPane = new JScrollPane(characters);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      // fudge the miniumum size to match the example.
      scrollPane.setMinimumSize(DluSupport.defaultInstance().createDimension(100,0));

      return scrollPane;
   }


   public static void main(String[] args)
   {
      JFrame f = new JFrame("Address Book Demo");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      AddressBookDemo panel = new AddressBookDemo();
      panel.setBorder(DluSupport.defaultInstance().createEmptyBorder(3));

      f.setContentPane(panel);
      f.pack();
      f.setLocationRelativeTo(null);
      f.setVisible(true);
   }
}

Answers to questions

  • Can it align components across other components? For example, can it both left and right align the 'phone' text field with the 'city' text field on the other side of the two address fields?

    Yes.
  • Does your manager know about platform specific gaps and spacing among components. Just curious.

    Yes (but not yet updated to integrate the changes in Java 6).
  • Can the text fields, labels, etc grow and shrink when the text is localized?

    Yes.
  • Just how much code do I have to write? Java code? Declarative XML?

    Java code.
  • Can I nest panels?

    Yes.
  • Can it align components along a baseline. For example, the new GroupLayout can align labels and text fields along the text baseline...that's nice.

    Yes (but not yet updated to use the baseline support in Java 6).
  • What's the licensing?

    LGPL.
  • Will this work for bi-directional locales? How difficult would it be to mirror the layout for a right-to-left locale?

    No. Slated for version 2.
  • What else should we evaluate?

    Debug Mode: The debug mode is useful.
    AddressBookDemoDebug.png

    Visibility Controls: The builder also lets you control the visibility of rows, columns and cells at runtime.
    Webstart : VisibilityDemo.jnlp

    // hide the whole row or column.
    builder.row(1).setVisible(false);
    builder.column(2).setVisible(false);
    
    // create a cell and configure it to consume no vertical space while hidden. 
    Cell cell = builder.add(myComponent, 2,3);
    cell.setHeightZeroIfInvisible(true);
    cell.setWidthZeroIfInvisible(false);
    
    // show or hide it, the layout will update according to the height and width
    // settings defined above.
    cell.setVisible(false);
    

    Spanning Support: The support for spanning components is also fairly sophisticated (read more).
    span-examples.png

Topic ExplicitTableBuilderExample . { Edit | Ref-By | Printable | Diffs r4 < r3 < r2 < r1 | More }
 XML java.net RSS

Revision r4 - 18 Apr 2005 - 22:55:26 - Main.pietschy
Parents: 3rdParty > LayoutManagerShowdown