Java Reference
In-Depth Information
The component for choosing the font is a Swing component of type JList that can display a list of any
type of component. 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 defined by the
JSplitPane class. Here the pane is split vertically but it can also hold two panels side by side. The
point size is displayed in another Swing component called a combobox, which is an object of type
JComboBox . The font style options are radio buttons so only one can be selected at any time. Finally,
we have two buttons to close the dialog.
We can start by defining the FontDialog class with its data members and its constructor, and then
build on that.
Try It Out - A FontDialog Class
The major work will be in the dialog class constructor. That will set up all the GUI elements as well as
the necessary listeners to respond to operations with the dialog. The dialog object will need to know
that the SketchFrame object that represents the Sketcher application window is the parent, so we will
pass a SketchFrame reference to the constructor.
Here's the code for the outline of the class:
// Class to define a dialog to choose a font
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.event.*;
class FontDialog extends JDialog implements Constants {
// Constructor
public FontDialog(SketchFrame window) {
// Code to initialize the data members...
// Code to create buttons and the button panel...
// Code to create the data input panel...
// Code to create the font choice and add it to the input panel...
// Code to create the font size choice
Search WWH ::




Custom Search