Java Reference
In-Depth Information
Creating the Font Dialog Buttons
Next you can add the code to the constructor that creates the button panel with the OK and Cancel buttons.
You can place the button panel at the bottom of the content pane for the dialog using the default Border-
Layout manager:
public FontDialog(SketcherFrame window) {
// Initialization as before...
// Create the dialog button panel
JPanel buttonPane = new JPanel();
// Create a panel to hold
buttons
// Create and add the buttons to the buttonPane
buttonPane.add(ok = createButton("OK")); // Add the OK button
buttonPane.add(cancel = createButton("Cancel")); // Add the Cancel button
getContentPane().add(buttonPane, BorderLayout.SOUTH);// Add pane
// Plus the code for the rest of the constructor...
}
Directory "Sketcher 5 displaying a font dialog"
The buttonPane object has a FlowLayout manager by default, so this takes care of positioning the but-
tons. You add the button pane to the dialog content pane using the BorderLayout.SOUTH specification to
place it at the bottom of the window. Because creating each button involves several steps that are the same
for both buttons, you are using a helper method, createButton() , that requires only the button label as
an argument. The code implies that you store each button reference in a class field, so you must add these
members to the FontDialog class:
private JButton ok;
// OK button
private JButton cancel;
// Cancel button
You use these fields in the listener for the button events.
You can code the createButton() method as a member of the FontDialog class as follows:
JButton createButton(String label) {
JButton button = new JButton(label);
// Create the button
button.setPreferredSize(new Dimension(80,20));
// Set the size
button.addActionListener(this);
// Listener is the dialog
return button;
// Return the button
}
Directory "Sketcher 5 displaying a font dialog"
Search WWH ::




Custom Search