Java Reference
In-Depth Information
constraints.gridwidth = GridBagConstraints.REMAINDER;
gbLayout.setConstraints(label, constraints);
dataPane.add(label);
// Plus the code for the rest of the constructor...
}
Directory "Sketcher 5 displaying a font dialog"
With the
fill
constraint set as
HORIZONTAL
, the components in a row fill the width of the
dataPane
con-
tainer, but without affecting the height. With the width constraint set to
REMAINDER
, the
label
component
fills the width of the row.
Implementing the Font List
You add the
JList<String>
object that displays the list of fonts next, but you won't add this directly to
the
dataPane
panel because the list is likely to be long enough to need scrolling capability. You can obtain
the list of fonts using the
GraphicsEnvironment
object that encapsulates information about the system in
which the application is running. Recall that you call a
static
method in the
GraphicsEnvironment
class
to get the
GraphicsEnvironment
object. Here's the code to create the list of font names:
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...
// Code to set up font list choice component
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = e.getAvailableFontFamilyNames(); // Get font names
fontList = new JList<>(fontNames); // Create list of font names
fontList.setValueIsAdjusting(true); // single event selection
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontList.setSelectedValue(font.getFontName(),true);
fontList.addListSelectionListener(this);
fontList.setToolTipText("Choose a font");
JScrollPane chooseFont = new JScrollPane(fontList); // Scrollable list
chooseFont.setMinimumSize(new Dimension(400,100));
chooseFont.setWheelScrollingEnabled(true);
// Enable mouse wheel
scroll
// Plus the code for the rest of the constructor...
}
Directory "Sketcher 5 displaying a font dialog"