Java Reference
In-Depth Information
// and add it to the input panel...
// Code to create the font style checkboxes
// and add them to the input panel...
// ...and then some!
}
private Font font; // Currently selected font
private int fontStyle; // Font style - Plain,Bold,Italic
private int fontSize; // Font point size
}
We will be adding a few more data members shortly, but at least we know we will need these. The code
to initialize the data members within the FontDialog constructor of the font dialog is easy. We will
initialize the font member and the associated fontStyle and fontSize members from the current
font in the application window:
public FontDialog(SketchFrame window) {
// Call the base constructor to create a modal dialog
super(window, "Font Selection", true);
font = window.getCurrentFont(); // Get the current font
fontStyle = font.getStyle(); // ...style
fontSize = font.getSize(); // ...and size
// Plus the code for the rest of the constructor...
}
We call the base class constructor and pass the window object to it as parent. The second argument is
the title for the dialog and the third argument determines that it is modal. The getCurrentFont()
method returns the font stored in the window object, and we use this to initialize the fontStyle and
fontSize members, so the first time we open the dialog this will be the default setting.
Creating the Buttons
Next we can add the code to the constructor that will create the button panel with the OK and Cancel
buttons. We will place this at the bottom of the content pane for the dialog using the default
BorderLayout manager:
public FontDialog(SketchFrame 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 to content pane
// Plus the code for the rest of the constructor...
}
Search WWH ::




Custom Search