The Artima Developer Community
Sponsored Link

Software and Return on Investment
Packer Fans are not always football fans
by Gregg Wonderly
March 18, 2005
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/

Talk Back!

Have an opinion? Be the first to post a comment about this weblog entry.

RSS Feed

If you'd like to be notified whenever Gregg Wonderly adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Gregg Wonderly graduated from Oklahoma State University in 1988 with an MS in COMSCI. His areas of concentration include Operating Systems and Languages. His first job was at the AT&T Bell Labs facilities in Naperville IL working on software retrofit for the 5ESS switch. He designed a procedure control language in the era of the development of Java with similar motivations that the Oak and then Java language development was driven by. Language design is still at the top of his list, but his focus tends to be on application languges layered on top of programming languages such as Java. Some just consider this API design, but there really is more to it! Gregg now works for Cyte Technologies Inc., where he does software engineering and design related to distributed systems in highly available environments.

This weblog entry is Copyright © 2005 Gregg Wonderly. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use