|
|
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
Page 4 of 8
public class CustomerPanel extends LabelledItemPanel
{
private JTextField myCustomerCodeField = new JTextField();
private JTextField myNameField = new JTextField();
private JTextArea myAddressField = new JTextArea(3, 20);
...
public CustomerPanel()
{
init();
}
private void init()
{
setBorder(BorderFactory.createEtchedBorder());
addItem("Customer Code", myCustomerCodeField);
addItem("Name", myNameField);
addItem("Address", new JScrollPane(myAddressField,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
...
}
public CustomerData getCustomerData()
{
CustomerData customerData = new CustomerData();
customerData.myCustomerCode = myCustomerCodeField.getText();
customerData.myName = myNameField.getText();
customerData.myAddress = myAddressField.getText();
...
return customerData;
}
...
}
// A method in some class using the CustomerPanel
{
CustomerPanel panel = new CustomerPanel();
// Display the panel to the User in a dialog
CustomerData customerData = panel.getCustomerData();
// Process the data
}
This section explains how LabelledItemPanel works. The code for the class is shown below:
public class LabelledItemPanel extends JPanel
{
/** The row to add the next labelled item to */
private int myNextItemRow = 0;
public LabelledItemPanel()
{
init();
}
private void init()
{
setLayout(new GridBagLayout());
// Create a blank label to use as a vertical fill so that the
// label/item pairs are aligned to the top of the panel and are not
// grouped in the centre if the parent component is taller than
// the preferred size of the panel.
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 99;
constraints.insets = new Insets(10, 0, 0, 0);
constraints.weighty = 1.0;
constraints.fill = GridBagConstraints.VERTICAL;
JLabel verticalFillLabel = new JLabel();
add(verticalFillLabel, constraints);
}
public void addItem(String labelText, JComponent item)
{
// Create the label and its constraints
JLabel label = new JLabel(labelText);
GridBagConstraints labelConstraints = new GridBagConstraints();
labelConstraints.gridx = 0;
labelConstraints.gridy = myNextItemRow;
labelConstraints.insets = new Insets(10, 10, 0, 0);
labelConstraints.anchor = GridBagConstraints.NORTHEAST;
labelConstraints.fill = GridBagConstraints.NONE;
add(label, labelConstraints);
// Add the component with its constraints
GridBagConstraints itemConstraints = new GridBagConstraints();
itemConstraints.gridx = 1;
itemConstraints.gridy = myNextItemRow;
itemConstraints.insets = new Insets(10, 10, 0, 10);
itemConstraints.weightx = 1.0;
itemConstraints.anchor = GridBagConstraints.WEST;
itemConstraints.fill = GridBagConstraints.HORIZONTAL;
add(item, itemConstraints);
myNextItemRow++;
}
}
The class extends Swing's JPanel and has two additional methods init() and addItem(). The init() is called from the constructor and sets up GridBagLayout used by the panel. We have already shown the second method, addItem(), being used. This method creates a JLabel for the label text and then adds JLabel and passed-in item to the panel with the correct GridBagConstraints. The constraints are set up so that the label text and the item are properly aligned, and that the item extends to the dialog's
right-hand edge.