1
|
|
package example;
|
2
|
|
|
3
|
|
import java.awt.*;
|
4
|
|
import java.awt.event.*;
|
5
|
|
import java.beans.*;
|
6
|
|
|
7
|
|
import javax.swing.*;
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
public class NumberChooser extends JPanel implements ActionListener {
|
14
|
|
protected ArrowButton up, down;
|
15
|
|
protected JTextField field;
|
16
|
|
|
17
|
0
|
public NumberChooser () {
|
18
|
0
|
setLayout (new BorderLayout ());
|
19
|
0
|
JPanel buttons = new JPanel(new GridLayout(0, 1));
|
20
|
0
|
down = new ArrowButton(ArrowButton.DOWN);
|
21
|
0
|
down.addActionListener (this);
|
22
|
0
|
add (field = new JTextField(4), BorderLayout.CENTER);
|
23
|
0
|
field.addActionListener (this);
|
24
|
0
|
up = new ArrowButton(ArrowButton.UP);
|
25
|
0
|
up.addActionListener (this);
|
26
|
0
|
buttons.add (up);
|
27
|
0
|
buttons.add (down);
|
28
|
0
|
add(buttons, BorderLayout.EAST);
|
29
|
|
}
|
30
|
0
|
public NumberChooser(int min, int max, int value){
|
31
|
0
|
this();
|
32
|
0
|
setMinimum(min);
|
33
|
0
|
setMaximum(max);
|
34
|
0
|
setValue(value);
|
35
|
|
}
|
36
|
|
|
37
|
0
|
public void requestFocus () {
|
38
|
0
|
field.requestFocus ();
|
39
|
|
}
|
40
|
|
|
41
|
0
|
public void setColumns (int c) {
|
42
|
0
|
field.setColumns (c);
|
43
|
|
}
|
44
|
|
|
45
|
0
|
public int getColumns () {
|
46
|
0
|
return field.getColumns ();
|
47
|
|
}
|
48
|
|
|
49
|
0
|
public synchronized void setValue (int v) {
|
50
|
0
|
field.setText (String.valueOf (v));
|
51
|
0
|
fireValueChange (getValue ());
|
52
|
|
}
|
53
|
|
|
54
|
0
|
public int getValue () {
|
55
|
0
|
try {
|
56
|
0
|
return clamp (Integer.parseInt (field.getText ()));
|
57
|
|
} catch (NumberFormatException ex) {
|
58
|
0
|
return clamp (0);
|
59
|
|
}
|
60
|
|
}
|
61
|
|
|
62
|
|
protected int minimum = Integer.MIN_VALUE;
|
63
|
|
|
64
|
0
|
public synchronized void setMinimum (int m) {
|
65
|
0
|
minimum = m;
|
66
|
|
}
|
67
|
|
|
68
|
0
|
public int getMinimum () {
|
69
|
0
|
return minimum;
|
70
|
|
}
|
71
|
|
|
72
|
|
protected int maximum = Integer.MAX_VALUE;
|
73
|
|
|
74
|
0
|
public synchronized void setMaximum (int m) {
|
75
|
0
|
maximum = m;
|
76
|
|
}
|
77
|
|
|
78
|
0
|
public int getMaximum () {
|
79
|
0
|
return maximum;
|
80
|
|
}
|
81
|
|
|
82
|
0
|
protected int clamp (int v) {
|
83
|
0
|
return Math.max (minimum, Math.min (maximum, v));
|
84
|
|
}
|
85
|
|
|
86
|
|
protected int step = 1;
|
87
|
|
|
88
|
0
|
public synchronized void setStep (int s) {
|
89
|
0
|
if (s <= 0)
|
90
|
0
|
throw new IllegalArgumentException ("Step too small (" + s + ").");
|
91
|
0
|
step = s;
|
92
|
|
}
|
93
|
|
|
94
|
0
|
public int getStep () {
|
95
|
0
|
return step;
|
96
|
|
}
|
97
|
|
|
98
|
0
|
public synchronized void actionPerformed (ActionEvent e) {
|
99
|
0
|
int value = getValue ();
|
100
|
0
|
if (e.getSource () == down) {
|
101
|
0
|
if (value > minimum) {
|
102
|
0
|
value = (value - step > value) ? minimum : clamp (value - step);
|
103
|
0
|
setValue (value);
|
104
|
0
|
fireValueChange (value);
|
105
|
|
}
|
106
|
0
|
} else if (e.getSource () == up) {
|
107
|
0
|
if (value < maximum) {
|
108
|
0
|
value = (value + step < value) ? maximum : clamp (value + step);
|
109
|
0
|
setValue (value);
|
110
|
0
|
fireValueChange (value);
|
111
|
|
}
|
112
|
0
|
} else if (e.getSource () == field) {
|
113
|
0
|
try {
|
114
|
0
|
int v = Integer.parseInt (e.getActionCommand ());
|
115
|
0
|
if ((v < minimum) || (v > maximum))
|
116
|
0
|
getToolkit ().beep ();
|
117
|
|
else
|
118
|
0
|
fireValueChange (v);
|
119
|
|
} catch (NumberFormatException ex) {
|
120
|
0
|
getToolkit ().beep ();
|
121
|
|
}
|
122
|
|
}
|
123
|
|
}
|
124
|
|
|
125
|
|
protected PropertyChangeSupport listeners =
|
126
|
|
new PropertyChangeSupport (this);
|
127
|
|
|
128
|
0
|
public void addPropertyChangeListener (PropertyChangeListener l) {
|
129
|
0
|
listeners.addPropertyChangeListener (l);
|
130
|
|
}
|
131
|
|
|
132
|
0
|
public void removePropertyChangeListener (PropertyChangeListener l) {
|
133
|
0
|
listeners.removePropertyChangeListener (l);
|
134
|
|
}
|
135
|
|
|
136
|
|
Integer oValue = new Integer (0);
|
137
|
|
|
138
|
0
|
protected void fireValueChange (int v) {
|
139
|
0
|
listeners.firePropertyChange ("value", oValue,
|
140
|
|
oValue = new Integer (v));
|
141
|
|
}
|
142
|
|
|
143
|
0
|
public static void main(String[] args){
|
144
|
0
|
try {
|
145
|
0
|
final JFrame frame = new JFrame("NumberChooser unit test");
|
146
|
0
|
frame.addWindowListener(new WindowAdapter() {
|
147
|
0
|
public void windowClosing(WindowEvent e) {
|
148
|
0
|
Window w = e.getWindow();
|
149
|
0
|
w.setVisible(false);
|
150
|
0
|
w.dispose();
|
151
|
0
|
System.exit(0);
|
152
|
|
}
|
153
|
|
});
|
154
|
0
|
JPanel panel = new JPanel(new GridLayout(0, 1));
|
155
|
0
|
final JLabel label = new JLabel("Enter a value");
|
156
|
0
|
NumberChooser chooser = new NumberChooser();
|
157
|
0
|
panel.add(label);
|
158
|
0
|
panel.add(chooser);
|
159
|
0
|
chooser.addPropertyChangeListener(new PropertyChangeListener() {
|
160
|
0
|
public void propertyChange(PropertyChangeEvent ev) {
|
161
|
0
|
System.out.println(((Integer)ev.getNewValue()).intValue());
|
162
|
|
}
|
163
|
|
});
|
164
|
0
|
frame.getContentPane().add(panel);
|
165
|
0
|
frame.pack();
|
166
|
0
|
frame.setVisible(true);
|
167
|
|
}
|
168
|
|
catch (Exception exc) {
|
169
|
0
|
System.out.println("Exception: " + exc.getMessage());
|
170
|
0
|
exc.printStackTrace();
|
171
|
0
|
System.exit(1);
|
172
|
|
}
|
173
|
|
}
|
174
|
|
}
|
175
|
|
|