Java Reference
In-Depth Information
Example 10•18: FontChooser.java (continued)
// The names to appear in the "Style" menu
static final String[] styleNames = new String[] {
"Plain", "Italic", "Bold", "BoldItalic"
};
// The style values that correspond to those names
static final Integer[] styleValues = new Integer[] {
new Integer(Font.PLAIN), new Integer(Font.ITALIC),
new Integer(Font.BOLD), new Integer(Font.BOLD+Font.ITALIC)
};
// The size "names" to appear in the size menu
static final String[] sizeNames = new String[] {
"8", "10", "12", "14", "18", "20", "24", "28", "32",
"40", "48", "56", "64", "72"
};
// This is the default preview string displayed in the dialog box
static final String defaultPreviewString =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" +
"abcdefghijklmnopqrstuvwxyz\n" +
"1234567890!@#$%ˆ&*()_-=+[]{}<,.>\n" +
"The quick brown fox jumps over the lazy dog";
/** Create a font chooser dialog for the specified frame. */
public FontChooser(Frame owner) {
super(owner, "Choose a Font"); // Set dialog frame and title
// This dialog must be used as a modal dialog. In order to be used
// as a modeless dialog, it would have to fire a PropertyChangeEvent
// whenever the selected font changed, so that applications could be
// notified of the user's selections.
setModal(true);
// Figure out what fonts are available on the system
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
fontFamilies = env.getAvailableFontFamilyNames();
// Set initial values for the properties
family = fontFamilies[0];
style = Font.PLAIN;
size = 18;
selectedFont = new Font(family, style, size);
// Create ItemChooser objects that allow the user to select font
// family, style, and size.
families = new ItemChooser("Family", fontFamilies, null, 0,
ItemChooser.COMBOBOX);
styles = new ItemChooser("Style", styleNames, styleValues, 0,
ItemChooser.COMBOBOX);
sizes = new ItemChooser("Size", sizeNames,null,4,ItemChooser.COMBOBOX);
// Now register event listeners to handle selections
families.addItemChooserListener(new ItemChooser.Listener() {
public void itemChosen(ItemChooser.Event e) {
setFontFamily((String)e.getSelectedValue());
}
});
styles.addItemChooserListener(new ItemChooser.Listener() {
Search WWH ::




Custom Search