Java Reference
In-Depth Information
textFont = font;
}
Directory "Sketcher 5 displaying a font dialog"
Now let's get back to the
FontDialog
constructor.
Adding the Data Pane
You can now add a panel to contain the components that receive input. You use a
JList<String>
object for
the font names, a
JSpinner
object for the point size of the font, and two
JRadioButton
objects for selecting
the font style. You can add the code to create the panel first:
public FontDialog(SketcherFrame window) {
// Initialization as before...
// Button panel code as before...
// Code to create the data input panel
JPanel dataPane = new JPanel(); // Create the data entry panel
dataPane.setBorder(BorderFactory.createCompoundBorder( // Pane border
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagLayout gbLayout = new GridBagLayout(); // Create the layout
dataPane.setLayout(gbLayout); // Set the pane layout
GridBagConstraints constraints = new GridBagConstraints();
// Plus the code for the rest of the constructor...
}
Directory "Sketcher 5 displaying a font dialog"
Here you use a
GridBagLayout
manager so you can set constraints for each component that you add to
the
dataPane
container. You also set a black line border for
dataPane
with an inset empty border 5 pixels
wide. This uses the
BorderFactory
static methods that you have seen before. You have many other possible
layout managers that you could use here.
BoxLayout
managers, for example, are very easy to use to lay out
components in vertical columns and horizontal rows.
The first component that you add to
dataPane
is a label that prompts for the font selection:
public FontDialog(SketcherFrame window) {
// Initialization as before...
// Button panel code as before...
// Set up the data input panel to hold all input components as before...
// Create the font choice and add it to the input panel
JLabel label = new JLabel("Choose a Font");
constraints.fill = GridBagConstraints.HORIZONTAL;