Java Reference
In-Depth Information
public void stateChanged(ChangeEvent e) {
Number value = (Number)((JSpinner)e.getSource()).getValue();
}
Directory "Sketcher 5 displaying a font dialog"
You're not really interested in a
Number
object, though. What you want is the integer value it contains, so
you can store it in the
fontSize
member of the dialog and then derive a new font. The
intValue()
method
for the
Number
object produces that. You can therefore arrive at the final version of
setChanged()
that does
what you want:
public void stateChanged(ChangeEvent e) {
int fontSize = ((Number)(((JSpinner)e.getSource()).getValue())).intValue();
font = font.deriveFont((float)fontSize);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
Directory "Sketcher 5 displaying a font dialog"
That first statement looks quite daunting, but because you put it together one step at a time, you should
see that it isn't really difficult — there are just a lot of parentheses to keep in sync.
Using Radio Buttons to Select the Font Style
Two
JRadioButton
objects provide the means for selecting the font style. One selects bold or not, and the
other selects italic or not. A plain font is simply one that is neither bold nor italic. You could use
JCheckBox
objects here if you prefer — they would work just as well. The radio buttons need to be fields in the dialog
class because you update them in the
actionPerformed()
method. Add them to the
FontDialog
class:
private JRadioButton bold;
// Bold style button
private JRadioButton italic;
// Italic style button
Here's the code to set them up:
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...
// Add the font choice prompt label as before...
// Set up font list choice component as before...
// Panel to display font sample as before...
// Create a split pane with font choice at the top as before...