001: package example;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.beans.*;
006:
007: import javax.swing.*;
008: import javax.swing.border.EmptyBorder;
009:
010: /**
011: * Select a font
012: */
013:
014: public class FontChooser extends JPanel implements
015: PropertyChangeListener, ItemListener {
016: protected Font specifiedFont = new Font("Serif", Font.PLAIN, 10);
017: protected JComboBox name;
018: protected JCheckBox bold, italic;
019: protected NumberChooser size;
020: protected static String[] availableFonts = null;
021:
022: public FontChooser() {
023: setLayout(new GridLayout(2, 2));
024: if (availableFonts == null) {
025: GraphicsEnvironment ge = GraphicsEnvironment
026: .getLocalGraphicsEnvironment();
027: availableFonts = ge.getAvailableFontFamilyNames();
028: }
029: name = new JComboBox(availableFonts);
030: name.setSelectedItem(specifiedFont.getName());
031: name.addItemListener(this );
032: add(name);
033: size = new NumberChooser(1, 128, specifiedFont.getSize());
034: add(size);
035: size.setColumns(3);
036: size.addPropertyChangeListener(this );
037: add(bold = new JCheckBox("bold"));
038: bold.setSelected(specifiedFont.isBold());
039: bold.addItemListener(this );
040: add(italic = new JCheckBox("italic"));
041: italic.setSelected(specifiedFont.isItalic());
042: italic.addItemListener(this );
043: }
044:
045: public void setSpecifiedFont(Font f) {
046: name.setSelectedItem(f.getName());
047: bold.setSelected(f.isBold());
048: italic.setSelected(f.isItalic());
049: size.setValue(f.getSize());
050: fireStateChange();
051: }
052:
053: public Font getSpecifiedFont() {
054: return new Font((String) name.getSelectedItem(), (bold
055: .isSelected() ? Font.BOLD : 0)
056: | (italic.isSelected() ? Font.ITALIC : 0), size
057: .getValue());
058: }
059:
060: public void itemStateChanged(ItemEvent e) {
061: fireStateChange();
062: }
063:
064: public void propertyChange(PropertyChangeEvent e) {
065: fireStateChange();
066: }
067:
068: protected PropertyChangeSupport listeners = new PropertyChangeSupport(
069: this );
070:
071: public void addPropertyChangeListener(PropertyChangeListener l) {
072: listeners.addPropertyChangeListener(l);
073: }
074:
075: public void removePropertyChangeListener(PropertyChangeListener l) {
076: listeners.removePropertyChangeListener(l);
077: }
078:
079: /** update our internal font, then tell everyone about it */
080: protected void fireStateChange() {
081: Font oldFont = specifiedFont;
082: specifiedFont = getSpecifiedFont();
083: listeners.firePropertyChange("style", oldFont, specifiedFont);
084: }
085:
086: /** Put up a frame containing a font chooser to make it easy for a script
087: * to play with.
088: */
089: public static void main(String[] args) {
090: final JFrame frame = new JFrame("Font Chooser unit test");
091: frame.addWindowListener(new WindowAdapter() {
092: public void windowClosing(WindowEvent e) {
093: frame.dispose();
094: }
095: });
096: final JPanel panel = new JPanel(new BorderLayout());
097: //panel.setBorder(new EmptyBorder(10, 10, 10, 10));
098: String text = "The quick brown fox jumped over the lazy dog";
099: FontChooser chooser = new FontChooser();
100: panel.add(chooser, BorderLayout.NORTH);
101: final JLabel label = new JLabel(text);
102: panel.add(label, BorderLayout.CENTER);
103: label.setFont(chooser.getSpecifiedFont());
104: frame.getContentPane().add(panel);
105: ((JPanel) frame.getContentPane()).setBorder(new EmptyBorder(4,
106: 4, 4, 4));
107:
108: // Position the frame away from the screen edge to avoid stupid
109: // toolbars and such
110: frame.setLocation(new Point(50, 50));
111: frame.setSize(400, 300);
112: frame.pack();
113:
114: Dimension s1 = panel.getPreferredSize();
115: Dimension s2 = frame.getPreferredSize();
116: final int hoff = s2.height - s1.height;
117: final int width = s2.width;
118: chooser.addPropertyChangeListener(new PropertyChangeListener() {
119: public void propertyChange(PropertyChangeEvent ev) {
120: label.setFont((Font) ev.getNewValue());
121: if (frame != null) {
122: Dimension size = panel.getPreferredSize();
123: size.height += hoff;
124: size.width = width;
125: frame.setSize(size);
126: }
127: }
128: });
129: frame.setVisible(true);
130: }
131: }
|