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 3 of 8

    JTextField customerCodeField = new JTextField();
    JTextField nameField = new JTextField();
    JTextArea addressField = new JTextArea(3, 20);
    ...

LabelledItemPanel panel = new LabelledItemPanel()
panel.setBorder(BorderFactory.createEtchedBorder());
panel.addItem("Customer Code", customerCodeField); panel.addItem("Name", nameField); panel.addItem("Address", new JScrollPane(addressField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); ...
// Display the panel to the User in a dialog
String customerCode = customerCodeField.getText(); String name = nameField.getText(); String address = addressField.getText(); ...
// Process the data


If you compare the simple addItem() method calls with the IDE-generated code shown below, the time-saving advantages of using this class are quite obvious:

  this.add(myCustomerCodeLabel,
    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
      GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
      new Insets(10, 10, 0, 0), 0, 0));
  this.add(myCustomerCodeField,
     new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
       GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
       new Insets(10, 10, 0, 10), 0, 0));
  this.add(myNameLabel, 
    new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
      GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
      new Insets(10, 0, 0, 0), 0, 0));


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

  • Print
  • Feedback

Resources