Java Reference
In-Depth Information
public void setCurrentFont(Font font) {
this.font = font;
}
Let's now get back to the FontDialog constructor.
Adding the Data Pane
We can now add a panel to contain the components that will receive input. We will have a JList
object for the font names, a JComboBox object for the point size of the font and three JRadioButton
objects for selecting the font style. We will add the code to create the panel first:
public FontDialog(SketchFrame 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( // Create 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...
}
Here we use a GridBagLayout manager so we can set constraints for each component that we add to
the dataPane container. We also set a black line border for dataPane with an inset empty border five
pixels wide. This uses the BorderFactory static methods that you have seen before.
The first component we will add to dataPane is a label prompting for the font selection:
public FontDialog(SketchFrame window) {
// Initialization as before...
// Button panel code as before...
// Set up the data input panel to hold all input components as before...
// Code to create the font choice and add it to the input panel
JLabel label = new JLabel("Choose a Font");
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gbLayout.setConstraints(label, constraints);
dataPane.add(label);
// Plus the code for the rest of the constructor...
}
With the fill constraint set as HORIZONTAL , the components in a row will fill the width of the
dataPane container, but without affecting the height. With the width constraint set to REMAINDER , the
label component will fill the width of the row.
Search WWH ::




Custom Search