The Artima Developer Community
Sponsored Link

Weblogs Forum
Packer Fans are not always football fans

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Packer Fans are not always football fans (View in Weblogs)
Posted: Mar 18, 2005 2:15 PM
Reply to this message Reply
Summary
Ben Galbraith has a great swing related webcast at JavaLobby which has a lot of good information about swing. He covers my Packer Layout Manager, and I want to reinforce some of the things he covered.
Advertisement

Packer and Programming Idioms

Idioms in program structure are an important part of maintaining your software and being able to easily alter and extend your software. The Packer class extends GridBagLayout and hides the details of managing the GridBagConstraints object by providing method based access to setting the various parameters. All of these methods return "this" so that you can chain invocations to further similify use.

The simple example is to create a labeled text field with the text field expanding to to fill all the width available to it.

  JPanel p = new JPanel();
  Packer pk = new Packer(p);

  int y = -1;
  pk.pack( new JLabel("Data:") ).gridx(0).gridy(++y);
  pk.pack( field ).gridx(1).gridy(y).fillx();
I don't want to cover Packer's operations here, really. The web site, packer.dev.java.net provides access to the javadocs and the source for your perusal.

The important idiom I want to mention is the use of the int y = -1; initialization and then the subsequent pattern of

  pk.pack( new JLabel("Data:") ).gridx(0).gridy(++y);
  pk.pack( field ).gridx(1).gridy(y).fillx();
where the ++y argument, causes that value to move to the row that the label is on. Subsequently, the text field is placed on the correct line by using the current value of y.

This idiom, seems odd, but provides the simplifying behavior that you can group lines in a GUI this way, and then reorder the lines without having to make any changes to the gridy() argument.

  JTextField field = new JTextField(),
     user = new JTextField();
  JPasswordField passwd = new JPasswordField();
  JComboBox itembox = new JComboBox();
  fillItems(itembox);

  pk.pack( new JLabel("Data:") ).gridx(0).gridy(++y);
  pk.pack( field ).gridx(1).gridy(y).fillx().gridw(3);

  pk.pack( new JLabel("User:") ).gridx(0).gridy(++y);
  pk.pack( user).gridx(1).gridy(y).fillx();
  pk.pack( new JLabel("Password:") ).gridx(2).gridy(y);
  pk.pack( passwd ).gridx(3).gridy(y).fillx();

  pk.pack( new JLabel("Item:") ).gridx(0).gridy(++y);
  pk.pack( itembox ).gridx(1).gridy(y).fillx().gridw(3);
If I want to put a JSeparator between the Data: line and the user/password line, I just need to put in the line
  pk.pack( new JSeparator() ).gridx(0).gridy(++y).fillx().gridw(4).inset(10,10,10,10);
and I am done. There are many simple idioms like this that if you adapt them, you can really save everyone time in refactoring/reorganizing code for future adaptations.

Resources

Ben Galbraith's webcast is here:
http://javalobby.org/eps/galbraith-swing-1/

Topic: When the Browser is an Inadequate Interface Previous Topic   Next Topic Topic: Politics and Money


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us