Java Reference
In-Depth Information
Example 10•18: FontChooser.java (continued)
* Call this method after show() to obtain the user's selection. If the
* user used the "Cancel" button, this will return null
**/
public Font getSelectedFont() { return selectedFont; }
// These are other property getter methods
public String getFontFamily() { return family; }
public int getFontStyle() { return style; }
public int getFontSize() { return size; }
// The property setter methods are a little more complicated.
// Note that none of these setter methods update the corresponding
// ItemChooser components as they ought to.
public void setFontFamily(String name) {
family = name;
changeFont();
}
public void setFontStyle(int style) {
this.style = style;
changeFont();
}
public void setFontSize(int size) {
this.size = size;
changeFont();
}
public void setSelectedFont(Font font) {
selectedFont = font;
family = font.getFamily();
style = font.getStyle();
size = font.getSize();
preview.setFont(font);
}
// This method is called when the family, style, or size changes
protected void changeFont() {
selectedFont = new Font(family, style, size);
preview.setFont(selectedFont);
}
// Override this inherited method to prevent anyone from making us modeless
public boolean isModal() { return true; }
/** This inner class demonstrates the use of FontChooser */
public static class Demo {
public static void main(String[] args) {
// Create some components and a FontChooser dialog
final JFrame frame = new JFrame("demo");
final JButton button = new JButton("Push Me!");
final FontChooser chooser = new FontChooser(frame);
// Handle button clicks
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Pop up the dialog
chooser.show();
// Get the user's selection
Font font = chooser.getSelectedFont();
Search WWH ::




Custom Search