The Source for Java Technology Collaboration
User: Password:



Evan Summers

Evan Summers's Blog

Address Form with Gbc

Posted by evanx on October 26, 2006 at 09:21 PM | Comments (3)

Here's having a go at John's address form using gridbaglady.dev.java.net.

Here is the jumble of code...

public class AddressFormPanel extends JPanel {    
    JTextField firstName = new JTextField();
    JTextField lastName = new JTextField();
    JTextField phone = new JTextField();
    JTextField email = new JTextField();
    JTextField address1 = new JTextField();
    JTextField address2 = new JTextField();
    JTextField city = new JTextField();
    JTextField state = new JTextField();
    JTextField postalCode = new JTextField();
    JTextField country = new JTextField();
    
    JButton newButton = new JButton("New");
    JButton deleteButton = new JButton("Delete");
    JButton editButton = new JButton("Edit");
    JButton saveButton = new JButton("Save");
    JButton cancelButton = new JButton("Cancel");

    JList selectionList = new JList();
    
    JSplitPane splitPane = new JSplitPane();
    
    public AddressFormPanel() {
        super(new GridBagLayout());
        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.add(new JLabel("Last Name"), 
                Gbc.xyi(1, 0, 2).east());
        formPanel.add(lastName, 
                Gbc.xyi(2, 0, 2).horizontal());
        formPanel.add(new JLabel("First Name"), 
                Gbc.xyi(3, 0, 2));
        formPanel.add(firstName, 
                Gbc.xyi(4, 0, 2).horizontal());
        formPanel.add(new JLabel("Phone"), 
                Gbc.xyi(1, 1, 2).east());
        formPanel.add(phone, 
                Gbc.xyi(2, 1, 2).horizontal());
        JPanel emailPanel = new JPanel(new GridBagLayout());
        emailPanel.add(new JLabel("Email"), 
                Gbc.xyi(1, 0, 2));
        emailPanel.add(email, 
                Gbc.xyi(2, 0, 2).horizontal());
        formPanel.add(emailPanel, 
                Gbc.xyi(3, 1, 0).gridwidth(2).horizontal());        
        formPanel.add(new JLabel("Address 1"), 
                Gbc.xyi(1, 2, 2).east());
        formPanel.add(address1, 
                Gbc.xyi(2 , 2, 2).gridwidth(3).horizontal());
        formPanel.add(new JLabel("Address 2"), 
                Gbc.xyi(1, 3, 2).east());
        formPanel.add(address2, 
                Gbc.xyi(2, 3, 2).gridwidth(3).horizontal());
        formPanel.add(new JLabel("City"), 
                Gbc.xyi(1, 4, 2).east());
        formPanel.add(city, 
                Gbc.xyi(2, 4, 2).horizontal());
        formPanel.add(new JLabel("State"), 
                Gbc.xyi(1, 5, 2).east());
        formPanel.add(state, 
                Gbc.xyi(2, 5, 2).horizontal());
        formPanel.add(new JLabel("Postal Code"), 
                Gbc.xyi(3, 5, 2).east());
        formPanel.add(postalCode, 
                Gbc.xyi(4, 5, 2).horizontal());
        formPanel.add(new JLabel("Country"), 
                Gbc.xyi(1, 6, 2).east());
        formPanel.add(country, 
                Gbc.xyi(2, 6, 2).horizontal());
        JPanel buttonPanel = new JPanel(new GridBagLayout());
        buttonPanel.add(newButton, 
                Gbc.xyi(1, 0, 2));
        buttonPanel.add(deleteButton, 
                Gbc.xyi(2, 0, 2));
        buttonPanel.add(editButton, 
                Gbc.xyi(3, 0, 2));
        buttonPanel.add(saveButton, 
                Gbc.xyi(4, 0, 2));
        buttonPanel.add(cancelButton, 
                Gbc.xyi(5, 0, 2));
        JScrollPane scrollPane = new JScrollPane(selectionList);
        JPanel leftPanel = new JPanel(new GridBagLayout());
        JPanel rightPanel = new JPanel(new GridBagLayout());
        leftPanel.add(scrollPane, 
                Gbc.xyi(0, 0, 4).both());
        rightPanel.add(formPanel, 
                Gbc.xyi(0, 0, 4).horizontal().north());
        rightPanel.add(buttonPanel, 
                Gbc.xyi(0, 1, 4).south().top(8));
        rightPanel.add(new JPanel(), 
                Gbc.xyi(0, 2, 0).vertical());
        splitPane.setLeftComponent(leftPanel);
        splitPane.setRightComponent(rightPanel);
        splitPane.setDividerLocation(100);
        add(splitPane, 
                Gbc.xyi(0, 0, 2).both());
    }   
}

where Gbc.xyi() is a static convenience method with parameters gridx, gridy and an "inset" for all round.

Yes, it is like the Matrix. It's easy though because you got auto-completion, error highlighting et al, in your IDE, which you don't have if you were to edit this as an XML thingy. But it's probably easier and faster to use a GUI builder. Anyway, the more you use Gbc, the more second-nature it becomes - like with anything.

Here's the picture...

addressForm2.png

And a WebStart....

Launch (AddressFormDemo, 110k/440k, unsandboxed, Java5)

I find i gotta stick to one layout manager (the Matrix) otherwise i get very confused, very quickly.

To simplify layout, i reckon its best to have as many subpanels as possible. And a "spacer panel" or two is usually called for, certainly with Gbc.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • When you see code that is still repetitive, such as:


    formPanel.add(new JLabel("Postal Code"),
    Gbc.xyi(3, 5, 2).east());
    formPanel.add(postalCode,
    Gbc.xyi(4, 5, 2).horizontal());

    Don't you get the feeling it would be better to write as:


    .xyi(3, 5, 2).east()
    .addLabel("Postal Code")
    .xyi(4, 5, 2).horizontal()
    .add(postalCode)

    ?

    Posted by: tackline on October 27, 2006 at 07:04 AM


  • I have tried to dice and slice it different ways, but i reckon the closer we stay to standard Swing, the better. So Gbc is a just a convenience subclass of GridBagConstraints, to smooth that rough edge, and we still compose panels as usual ie panel.add(component, gbc).

    Posted by: evanx on October 27, 2006 at 05:14 PM

  • i've updated the demo with an alternative that uses GroupLayout, but tries to simplify the form into subpanels which are rows of subpanels with the texfield underneath it's label, which is how i do forms using Gbc in greenscreen. Read the tab labelled "QLayout" for more info on that. I'll write a blog entry on it sometime in the next week or so, cos i'll still playing with it.

    Posted by: evanx on November 12, 2006 at 12:09 PM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds