Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Speed up your Swing GUI construction with better building blocks

Use two helper classes to reduce dialog development time

  • Print
  • Feedback

Page 2 of 8

What is common? What is different?



We determined the following three elements were common to most of our dialogs:

  • The dialogs usually consisted of a list of items and labels neatly aligned down the page
  • Each dialog normally had Ok and Cancel buttons
  • The actions performed when the Ok and Cancel buttons were pressed were usually the same


The differences between screens were the actual fields being edited and their labels.

Better building blocks

The most tedious part of developing the dialogs was specifying the layout constraints for each field and its label. The fastest way to develop is for the classes to allow us to specify the fields and their labels without specifying the layout constraints. It seemed to make sense to use two different classes: one for handling layout of the labelled fields and one for the common dialog buttons and associated processing. We called the class for layout handling LabelledItemPanel, while we called the class for the common dialog features StandardDialog.

A common panel: LabelledItemPanel

The purpose of LabelledItemPanel is to lay out items and their labels neatly on a panel. The class uses a GridBagLayout to control the actual layout, but this is all hidden from class users. To use the class, the item and its label are added to the panel with the addItem() method, passing the text for the field label and the field component. The user does not have to specify any layout constraints because the class works out all of the layout constraints needed.

The LabelledItemPanel can be used in two ways:

  • Create an instance of the LabelledItemPanel and add data entry fields to the panel
  • Subclass LabelledItemPanel and add the data entry fields to the panel during construction


An example of using LabelledItemPanel directly is shown in the following code fragment:

  • Print
  • Feedback

Resources