1
|
|
package example;
|
2
|
|
|
3
|
|
import java.awt.*;
|
4
|
|
import java.awt.event.*;
|
5
|
|
import java.beans.*;
|
6
|
|
|
7
|
|
import javax.swing.*;
|
8
|
|
import javax.swing.border.EmptyBorder;
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
public class FontChooser extends JPanel
|
15
|
|
implements PropertyChangeListener, ItemListener {
|
16
|
|
protected Font specifiedFont = new Font ("Serif", Font.PLAIN, 10);
|
17
|
|
protected JComboBox name;
|
18
|
|
protected JCheckBox bold, italic;
|
19
|
|
protected NumberChooser size;
|
20
|
|
protected static String[] availableFonts = null;
|
21
|
|
|
22
|
0
|
public FontChooser () {
|
23
|
0
|
setLayout (new GridLayout (2, 2));
|
24
|
0
|
if (availableFonts == null){
|
25
|
0
|
GraphicsEnvironment ge =
|
26
|
|
GraphicsEnvironment.getLocalGraphicsEnvironment();
|
27
|
0
|
availableFonts = ge.getAvailableFontFamilyNames();
|
28
|
|
}
|
29
|
0
|
name = new JComboBox (availableFonts);
|
30
|
0
|
name.setSelectedItem (specifiedFont.getName ());
|
31
|
0
|
name.addItemListener (this);
|
32
|
0
|
add(name);
|
33
|
0
|
size = new NumberChooser(1, 128, specifiedFont.getSize());
|
34
|
0
|
add (size);
|
35
|
0
|
size.setColumns (3);
|
36
|
0
|
size.addPropertyChangeListener (this);
|
37
|
0
|
add (bold = new JCheckBox ("bold"));
|
38
|
0
|
bold.setSelected (specifiedFont.isBold ());
|
39
|
0
|
bold.addItemListener (this);
|
40
|
0
|
add (italic = new JCheckBox ("italic"));
|
41
|
0
|
italic.setSelected (specifiedFont.isItalic ());
|
42
|
0
|
italic.addItemListener (this);
|
43
|
|
}
|
44
|
|
|
45
|
0
|
public void setSpecifiedFont(Font f) {
|
46
|
0
|
name.setSelectedItem (f.getName ());
|
47
|
0
|
bold.setSelected (f.isBold ());
|
48
|
0
|
italic.setSelected (f.isItalic ());
|
49
|
0
|
size.setValue (f.getSize ());
|
50
|
0
|
fireStateChange ();
|
51
|
|
}
|
52
|
|
|
53
|
0
|
public Font getSpecifiedFont () {
|
54
|
0
|
return new Font ((String)name.getSelectedItem (),
|
55
|
0
|
(bold.isSelected() ? Font.BOLD: 0) |
|
56
|
0
|
(italic.isSelected() ? Font.ITALIC: 0),
|
57
|
|
size.getValue());
|
58
|
|
}
|
59
|
|
|
60
|
0
|
public void itemStateChanged (ItemEvent e) {
|
61
|
0
|
fireStateChange();
|
62
|
|
}
|
63
|
|
|
64
|
0
|
public void propertyChange (PropertyChangeEvent e) {
|
65
|
0
|
fireStateChange();
|
66
|
|
}
|
67
|
|
|
68
|
|
protected PropertyChangeSupport listeners =
|
69
|
|
new PropertyChangeSupport (this);
|
70
|
|
|
71
|
0
|
public void addPropertyChangeListener(PropertyChangeListener l) {
|
72
|
0
|
listeners.addPropertyChangeListener (l);
|
73
|
|
}
|
74
|
|
|
75
|
0
|
public void removePropertyChangeListener(PropertyChangeListener l) {
|
76
|
0
|
listeners.removePropertyChangeListener (l);
|
77
|
|
}
|
78
|
|
|
79
|
|
|
80
|
0
|
protected void fireStateChange() {
|
81
|
0
|
Font oldFont = specifiedFont;
|
82
|
0
|
specifiedFont = getSpecifiedFont();
|
83
|
0
|
listeners.firePropertyChange ("style", oldFont, specifiedFont);
|
84
|
|
}
|
85
|
|
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
0
|
public static void main(String[] args) {
|
90
|
0
|
final JFrame frame = new JFrame("Font Chooser unit test");
|
91
|
0
|
frame.addWindowListener(new WindowAdapter() {
|
92
|
0
|
public void windowClosing(WindowEvent e) {
|
93
|
0
|
frame.dispose();
|
94
|
|
}
|
95
|
|
});
|
96
|
0
|
final JPanel panel = new JPanel(new BorderLayout());
|
97
|
|
|
98
|
0
|
String text = "The quick brown fox jumped over the lazy dog";
|
99
|
0
|
FontChooser chooser = new FontChooser();
|
100
|
0
|
panel.add(chooser, BorderLayout.NORTH);
|
101
|
0
|
final JLabel label = new JLabel(text);
|
102
|
0
|
panel.add(label, BorderLayout.CENTER);
|
103
|
0
|
label.setFont(chooser.getSpecifiedFont());
|
104
|
0
|
frame.getContentPane().add(panel);
|
105
|
0
|
((JPanel)frame.getContentPane()).setBorder(new EmptyBorder(4,4,4,4));
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
0
|
frame.setLocation(new Point(50, 50));
|
110
|
0
|
frame.setSize(400, 300);
|
111
|
0
|
frame.pack();
|
112
|
|
|
113
|
0
|
Dimension s1 = panel.getPreferredSize();
|
114
|
0
|
Dimension s2 = frame.getPreferredSize();
|
115
|
0
|
final int hoff = s2.height - s1.height;
|
116
|
0
|
final int width = s2.width;
|
117
|
0
|
chooser.addPropertyChangeListener(new PropertyChangeListener() {
|
118
|
0
|
public void propertyChange(PropertyChangeEvent ev) {
|
119
|
0
|
label.setFont((Font)ev.getNewValue());
|
120
|
0
|
if (frame != null) {
|
121
|
0
|
Dimension size = panel.getPreferredSize();
|
122
|
0
|
size.height += hoff;
|
123
|
0
|
size.width = width;
|
124
|
0
|
frame.setSize(size);
|
125
|
|
}
|
126
|
|
}
|
127
|
|
});
|
128
|
0
|
frame.show();
|
129
|
|
}
|
130
|
|
}
|
131
|
|
|
132
|
|
|