Java Reference
In-Depth Information
Implementing the Font List
The next component we will add is the
JList
object that displays the list of fonts, but we won't add this
directly to the
dataPane
. The list of fonts will have to be obtained using the
GraphicsEnvironment
object that encapsulates information about the system in which the application is running. You will
remember that we must call a
static
method in the
GraphicsEnvironment
class to get the object.
Here's the code to create the list of font names:
public FontDialog(SketchFrame 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 the font names
fontList = new JList(fontNames); // Create list of font names
fontList.setValueIsAdjusting(true); // single event selection
fontList.setSelectionMode(ListSelectionModel.SINGLE
_
SELECTION); // Choose 1 font
fontList.setSelectedValue(font.getFamily(),true);
fontList.addListSelectionListener(this);
JScrollPane chooseFont = new JScrollPane(fontList); // Scrollable list
chooseFont.setMinimumSize(new Dimension(300,100));
chooseFont.setWheelScrollingEnabled(true); // Enable mouse wheel scroll
// Plus the code for the rest of the constructor...
}
We obtain the list of font family names for the system on which Sketcher is running, by calling the
getAvailableFontFamilyNames()
method for the
GraphicsEnvironment
object. The
fontList
variable will need to be accessible in the method handling events for the list, so this will be
another data member of the class:
private JList fontList; // Font list
The
fontNames
array holds
String
objects, but you can create a
JList
object for any kind of object,
images for example. You can also create a
JList
object by passing a
Vector
that contains the objects
you want in the list to the constructor. It is possible to allow multiple entries from a list to be selected, in
which case the selection process may cause multiple events - when you drag the cursor over several list
items for example. You can ensure that there is only one event for a selection, even though multiple
items are selected, by calling the
setValueIsAdjusting()
method with the argument
true
. Calling
setSelectionMode()
with the argument
SINGLE
_
SELECTION
ensures that only one font name can
be selected. You have two possible multiple selections you can enable.
Passing the value
SINGLE
_
INTERVAL
_
SELECTION
to the method allows a series of consecutive items
to be selected. Passing
MULTIPLE
_
SELECTION
_
INTERVAL
provides you with total flexibility and
allows any number of items anywhere to be selected. The initial selection in the list is set by the
setSelectedValue()
call. We pass the family name for the current font as the argument specifying
the initial selection. There is a complementary method,
getSelectedValue()
, that we will be using
in the event handler.