Sponsored Link •
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.
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
++yargument, 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
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
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
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.
Ben Galbraith's webcast is here:
Have an opinion? Be the first to post a comment about this weblog entry.
|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.|