/Users/lyon/j4p/src/gui/run/IntegerRunSliderPanel.java
|
1 package gui.run;
2
3 // Fig. 25.33: SliderFieldPanel.java
4 // A subclass of JPanel containing a JSlider and a JTextField
5
6
7 import gui.ClosableJFrame;
8
9 import javax.swing.JPanel;
10 import javax.swing.event.ChangeEvent;
11 import javax.swing.event.ChangeListener;
12 import java.awt.BorderLayout;
13 import java.awt.Container;
14 import java.awt.FlowLayout;
15 import java.awt.GridLayout;
16 import java.io.Serializable;
17
18 /**
19 * Only works for integers. Shows a textfield and
20 * a slider, as well as a label. Shortcut
21 * mnemonics are used in the label to focus onto
22 * the textfield.
23 *
24 * @see RunSlider
25 */
26 public class IntegerRunSliderPanel
27 extends JPanel
28 implements Serializable {
29 private RunTextFieldOld tf;
30 private int currentValue;
31
32 // object to support bound property changes
33 //private PropertyChangeSupport changeSupport;
34 private RunSlider slider;
35 private RunLabel nameLabel = null;
36
37 public IntegerRunSliderPanel(String name,
38 RunSlider rs) {
39 slider = rs;
40 nameLabel = new RunLabel(name);
41 // create PropertyChangeSupport for bound properties
42 //changeSupport = new PropertyChangeSupport(this);
43 tf = new RunTextFieldOld(
44 String.valueOf(slider.getValue()),
45 5) {
46 public void run() {
47 setValue(parseInt());
48 }
49 };
50 ShortcutUtils.addFocusAccelerator(name,
51 tf);
52 setLayout(new BorderLayout());
53 JPanel jp = new JPanel();
54 jp.setLayout(new GridLayout(0,1));
55 jp.add(tf);
56 jp.add(nameLabel);
57 add(jp, BorderLayout.WEST);
58 add(slider, BorderLayout.CENTER);
59
60 slider.addChangeListener(new ChangeListener() {
61 public void stateChanged(
62 ChangeEvent e) {
63 setValue(slider.getValue());
64 }
65 });
66 this.invalidate();
67 }
68
69 private int parseInt() {
70 try {
71 return Integer.parseInt(tf.getText());
72 } catch (NumberFormatException e) {
73 return 0;
74 }
75 }
76
77 // property minimumValue
78 public void setMinimumValue(int min) {
79 slider.setMinimum(min);
80
81 if (slider.getValue() <
82 slider.getMinimum()) {
83 slider.setValue(slider.getMinimum());
84 tf.setText(
85 String.valueOf(
86 slider.getValue()));
87 }
88 }
89
90 public int getMinimumValue() {
91 return slider.getMinimum();
92 }
93
94 // property maximumValue
95 public void setMaximumValue(int max) {
96 slider.setMaximum(max);
97
98 if (slider.getValue() >
99 slider.getMaximum()) {
100 slider.setValue(slider.getMaximum());
101 tf.setText(
102 String.valueOf(
103 slider.getValue()));
104 }
105 }
106
107 public int getMaximumValue() {
108 return slider.getMaximum();
109 }
110
111 /**
112 * Sets the current value of both the slider
113 * and the textfield
114 *
115 * @param n
116 */
117 public void setValue(int n) {
118 currentValue = n;
119 slider.setValue(currentValue);
120 tf.setText(String.valueOf(currentValue));
121 }
122
123 /**
124 * @return value of the slider
125 */
126 public int getValue() {
127 return slider.getValue();
128 }
129
130 public static void main(String[] args) {
131 ClosableJFrame cf = new ClosableJFrame();
132 Container c = cf.getContentPane();
133 c.setLayout(new FlowLayout());
134 c.add(new IntegerRunSliderPanel(
135 "slider [1",
136 new RunSlider(1, 1000) {
137 public void run() {
138 System.out.println(
139 getValue());
140
141 }
142 }));
143 c.add(new IntegerRunSliderPanel(
144 "s [2", new RunSlider(
145 -50, 50) {
146 public void run() {
147 }
148 }));
149 c.add(new IntegerRunSliderPanel(
150 "slider [3", new RunSlider(1, 5) {
151 public void run() {
152
153 }
154 }));
155 cf.setSize(400, 400);
156 cf.show();
157 }
158 }
159