|
|
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 7 of 8
public class CustomerDialog extends StandardDialog
{
private JTextField myCustomerCodeField = new JTextField();
private JTextField myNameField = new JTextField();
private JTextArea myAddressField = new JTextArea(3, 20);
...
private LabelledItemPanel myContentPane = new LabelledItemPanel();
public CustomerDialog()
{
init();
}
private void init()
{
setTitle("Customer Dialog");
myContentPane.setBorder(BorderFactory.createEtchedBorder());
myContentPane.addItem("Customer Code", myCustomerCodeField);
myContentPane.addItem("Name", myNameField);
myContentPane.addItem("Address", new JScrollPane(myAddressField,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
...
setContentPane(myContentPane);
}
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 CustomerDialog
{
CustomerDialog dialog = new CustomerDialog();
dialog.pack();
dialog.show();
if(!dialog.hasUserCancelled())
{
CustomerData customerData = dialog.getCustomerData();
// Process the data
}
}
If data validation needs to be performed before the dialog closes, then the isValidData() method should be overridden. An example of this is given below:
protected boolean isValidData()
{
if(myCustomerCodeField.getText().equals(""))
{
JOptionPane.showMessageDialog(this,
"Please enter a Customer Code",
"Blank Customer Code",
JOptionPane.WARNING_MESSAGE);
myCustomerCodeField.requestFocus();
return false;
}
if(myNameField.getText().equals(""))
{
JOptionPane.showMessageDialog(this,
"Please enter a Name",
"Blank Name",
JOptionPane.WARNING_MESSAGE);
myNameField.requestFocus();
return false;
}
return true;
}
This section explains how StandardDialog works. The appearance and behavior of StandardDialog is set up in the init() method shown below:
private void init()
{
setModal(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
// Setup the internal content pane to hold the user content pane
// and the standard button panel
JPanel internalContentPane = new JPanel();
internalContentPane.setLayout(
new BorderLayout(COMPONENT_SPACING, COMPONENT_SPACING));
internalContentPane.setBorder(
BorderFactory.createEmptyBorder(COMPONENT_SPACING,
COMPONENT_SPACING, COMPONENT_SPACING, COMPONENT_SPACING));
// Create the standard button panel with "Ok" and "Cancel"
Action okAction = new AbstractAction("Ok")
{
public void actionPerformed(ActionEvent actionEvent)
{
if(isValidData())
{
myIsDialogCancelled = false;
dispose();
}
}
};
Action cancelAction = new AbstractAction("Cancel")
{
public void actionPerformed(ActionEvent actionEvent)
{
myIsDialogCancelled = true;
dispose();
}
};
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
buttonPanel.add(new JButton(okAction));
buttonPanel.add(new JButton(cancelAction));
internalContentPane.add(buttonPanel, BorderLayout.SOUTH);
// Initialise the user content pane with a JPanel
setContentPane(new JPanel(new BorderLayout()));
super.setContentPane(internalContentPane);
// Finally, add a listener for the window close button.
// Process this event the same as the "Cancel" button.
WindowAdapter windowAdapter = new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
myIsDialogCancelled = true;
dispose();
}
};
addWindowListener(windowAdapter);
}
The init() is called from the constructor. The method does the following:
myIsDialogCancelled to indicate which button has been pressed and then call dispose, which causes the show() method to unblock. The method hasUserCancelled() checks the value of myIsDialogCancelled as shown in the usage example.Note: Download the full source code for LabelledItemPanel and StandardDialog from Resources.
We have saved substantial development time with this approach. The average time spent building a panel using an IDE GUI builder
was between one and three hours depending on the number of fields on the panel. Using our LabelledItemPanel class, the panels were constructed in less than 20 minutes. We have used LabelledItemPanel 35 times.
The other advantages of this approach include:
addItem() calls. You do not have to adjust constraints on all of the reordered fields and labels.Our simple classes could be further enhanced to include the following: