FontChooserpublic class FontChooser extends JDialog This is a JDialog subclass that allows the user to select a font, in any
style and size, from the list of available fonts on the system. The
dialog is modal. Display it with show(); this method does not return
until the user dismisses the dialog. When show() returns, call
getSelectedFont() to obtain the user's selection. If the user clicked the
dialog's "Cancel" button, getSelectedFont() will return null. |
Fields Summary |
---|
String | family | int | style | int | size | Font | selectedFont | String[] | fontFamilies | je3.gui.ItemChooser | families | je3.gui.ItemChooser | styles | je3.gui.ItemChooser | sizes | JTextArea | preview | JButton | okay | JButton | cancel | static final String[] | styleNames | static final Integer[] | styleValues | static final String[] | sizeNames | static final String | defaultPreviewString |
Constructors Summary |
---|
public FontChooser(Frame owner)Create a font chooser dialog for the specified frame.
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() {
public void itemChosen(ItemChooser.Event e) {
setFontStyle(((Integer)e.getSelectedValue()).intValue());
}
});
sizes.addItemChooserListener(new ItemChooser.Listener() {
public void itemChosen(ItemChooser.Event e) {
setFontSize(Integer.parseInt((String)e.getSelectedValue()));
}
});
// Create a component to preview the font.
preview = new JTextArea(defaultPreviewString, 5, 40);
preview.setFont(selectedFont);
// Create buttons to dismiss the dialog, and set handlers on them
okay = new JButton("Okay");
cancel = new JButton("Cancel");
okay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { hide(); }
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedFont = null;
hide();
}
});
// Put the ItemChoosers in a Box
Box choosersBox = Box.createHorizontalBox();
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(families);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(styles);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(sizes);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(Box.createGlue());
// Put the dismiss buttons in another box
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(Box.createGlue());
buttonBox.add(okay);
buttonBox.add(Box.createGlue());
buttonBox.add(cancel);
buttonBox.add(Box.createGlue());
// Put the choosers at the top, the buttons at the bottom, and
// the preview in the middle.
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(preview), BorderLayout.CENTER);
contentPane.add(choosersBox, BorderLayout.NORTH);
contentPane.add(buttonBox, BorderLayout.SOUTH);
// Set the dialog size based on the component size.
pack();
|
Methods Summary |
---|
protected void | changeFont()
selectedFont = new Font(family, style, size);
preview.setFont(selectedFont);
| public java.lang.String | getFontFamily() return family;
| public int | getFontSize() return size;
| public int | getFontStyle() return style;
| public java.awt.Font | getSelectedFont()Call this method after show() to obtain the user's selection. If the
user used the "Cancel" button, this will return null return selectedFont;
| public boolean | isModal() return true;
| public void | setFontFamily(java.lang.String name)
family = name;
changeFont();
| public void | setFontSize(int size)
this.size = size;
changeFont();
| public void | setFontStyle(int style)
this.style = style;
changeFont();
| public void | setSelectedFont(java.awt.Font font)
selectedFont = font;
family = font.getFamily();
style = font.getStyle();
size = font.getSize();
preview.setFont(font);
|
|