Java Reference
In-Depth Information
You want to be able to choose the font name from those available on the system on which the application
is executing. You also want to select the style of the font, whether plain, bold, or italic, as well as the point
size. It would also be nice to see what a font looks like before you decide to use it. The dialog therefore
needs to obtain a list of the fonts available and display them in a component. It also needs a component to
allow the point size to be selected and some means for choosing the style for the font.
This is not going to be a wimpy, pathetic excuse for a dialog like those you have seen so far. This is going
to be a real, chunky Java programmer's dialog. You drag in a diversity of components here, just for the
experience. Because it involves quite a lot of code, you build it step-by-step. Just so that you know where
you're headed, the finished dialog is shown in Figure 20-10 .
FIGURE 20-10
The component that provides the choice of font in Figure 20-10 is a Swing component of type
javax.swing.JList<T> that can display a list of objects of type T . Below that is a panel holding a JLabel
object, which displays a sample of the current font. The list of font names and the panel below are displayed
in a split pane that is defined by the JSplitPane class. Here the pane is split vertically, but a JSplitPane
object can also hold two panels side by side. The point size is displayed in another Swing component called a
spinner, which is an object of type javax.swing.JSpinner . The choice for the font style options is provided
by two radio buttons, and either, neither, or both may be selected, so they won't be in a button group. Finally,
you have two buttons to close the dialog.
You can set the foundations by defining the FontDialog class with its data members and its constructor,
and then build on that.
The FontDialog Class
Most of the work is in the dialog class constructor, which sets up all the GUI elements as well as the neces-
sary listeners to respond to user interactions with the dialog. The dialog object needs access to the Sketch-
erFrame object that represents the Sketcher application window and is the dialog's parent, so you pass a
SketcherFrame reference to the constructor.
Search WWH ::

Custom Search