1
36
37
40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.border.*;
46 import javax.swing.colorchooser.*;
47 import javax.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import java.awt.*;
51 import java.awt.event.*;
52 import java.beans.*;
53 import java.util.*;
54 import java.io.*;
55 import java.applet.*;
56 import java.net.*;
57
58
65 public class SliderDemo extends DemoModule {
66
67
70 public static void main(String[] args) {
71 SliderDemo demo = new SliderDemo(null);
72 demo.mainImpl();
73 }
74
75
78 public SliderDemo(SwingSet2 swingset) {
79 super(swingset, "SliderDemo", "toolbar/JSlider.gif");
82
83 createSliderDemo();
84 }
85
86 public void createSliderDemo() {
87 JSlider s;
88 JPanel hp;
89 JPanel vp;
90 GridLayout g;
91 JPanel tp;
92 JLabel tf;
93 ChangeListener listener;
94
95 getDemoPanel().setLayout(new BorderLayout());
96
97 tf = new JLabel(getString("SliderDemo.slidervalue"));
98 getDemoPanel().add(tf, BorderLayout.SOUTH);
99
100 tp = new JPanel();
101 g = new GridLayout(1, 2);
102 g.setHgap(5);
103 g.setVgap(5);
104 tp.setLayout(g);
105 getDemoPanel().add(tp, BorderLayout.CENTER);
106
107 listener = new SliderListener(tf);
108
109 BevelBorder border = new BevelBorder(BevelBorder.LOWERED);
110
111 hp = new JPanel();
112 hp.setLayout(new BoxLayout(hp, BoxLayout.Y_AXIS));
113 hp.setBorder(new TitledBorder(
114 border,
115 getString("SliderDemo.horizontal"),
116 TitledBorder.LEFT,
117 TitledBorder.ABOVE_TOP));
118 tp.add(hp);
119
120 vp = new JPanel();
121 vp.setLayout(new BoxLayout(vp, BoxLayout.X_AXIS));
122 vp.setBorder(new TitledBorder(
123 border,
124 getString("SliderDemo.vertical"),
125 TitledBorder.LEFT,
126 TitledBorder.ABOVE_TOP));
127 tp.add(vp);
128
129 JPanel p = new JPanel();
131 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
132 p.setBorder(new TitledBorder(getString("SliderDemo.plain")));
133 s = new JSlider(-10, 100, 20);
134 s.getAccessibleContext().setAccessibleName(getString("SliderDemo.plain"));
135 s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.a_plain_slider"));
136 s.addChangeListener(listener);
137
138 p.add(Box.createRigidArea(VGAP5));
139 p.add(s);
140 p.add(Box.createRigidArea(VGAP5));
141 hp.add(p);
142 hp.add(Box.createRigidArea(VGAP10));
143
144 p = new JPanel();
146 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
147 p.setBorder(new TitledBorder(getString("SliderDemo.majorticks")));
148 s = new JSlider(100, 1000, 400);
149 s.setPaintTicks(true);
150 s.setMajorTickSpacing(100);
151 s.getAccessibleContext().setAccessibleName(getString("SliderDemo.majorticks"));
152 s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.majorticksdescription"));
153 s.addChangeListener(listener);
154
155 p.add(Box.createRigidArea(VGAP5));
156 p.add(s);
157 p.add(Box.createRigidArea(VGAP5));
158 hp.add(p);
159 hp.add(Box.createRigidArea(VGAP10));
160
161 p = new JPanel();
163 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
164 p.setBorder(new TitledBorder(getString("SliderDemo.ticks")));
165 s = new JSlider(0, 11, 6);
166
167 s.putClientProperty("JSlider.isFilled", Boolean.TRUE );
168
169 s.setPaintTicks(true);
170 s.setMajorTickSpacing(5);
171 s.setMinorTickSpacing(1);
172
173 s.setPaintLabels( true );
174 s.setSnapToTicks( true );
175
176 s.getLabelTable().put(new Integer(11), new JLabel(new Integer(11).toString(), JLabel.CENTER));
177 s.setLabelTable( s.getLabelTable() );
178
179 s.getAccessibleContext().setAccessibleName(getString("SliderDemo.minorticks"));
180 s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.minorticksdescription"));
181
182 s.addChangeListener(listener);
183
184 p.add(Box.createRigidArea(VGAP5));
185 p.add(s);
186 p.add(Box.createRigidArea(VGAP5));
187 hp.add(p);
188 hp.add(Box.createRigidArea(VGAP10));
189
190 p = new JPanel();
192 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
193 p.setBorder(new TitledBorder(getString("SliderDemo.disabled")));
194 BoundedRangeModel brm = new DefaultBoundedRangeModel(80, 0, 0, 100);
195 s = new JSlider(brm);
196 s.setPaintTicks(true);
197 s.setMajorTickSpacing(20);
198 s.setMinorTickSpacing(5);
199 s.setEnabled(false);
200 s.getAccessibleContext().setAccessibleName(getString("SliderDemo.disabled"));
201 s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.disableddescription"));
202 s.addChangeListener(listener);
203
204 p.add(Box.createRigidArea(VGAP5));
205 p.add(s);
206 p.add(Box.createRigidArea(VGAP5));
207 hp.add(p);
208
209
211 p = new JPanel();
213 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
214 p.setBorder(new TitledBorder(getString("SliderDemo.plain")));
215 s = new JSlider(JSlider.VERTICAL, -10, 100, 20);
216 s.getAccessibleContext().setAccessibleName(getString("SliderDemo.plain"));
217 s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.a_plain_slider"));
218 s.addChangeListener(listener);
219 p.add(Box.createRigidArea(HGAP10));
220 p.add(s);
221 p.add(Box.createRigidArea(HGAP10));
222 vp.add(p);
223 vp.add(Box.createRigidArea(HGAP10));
224
225 p = new JPanel();
227 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
228 p.setBorder(new TitledBorder(getString("SliderDemo.majorticks")));
229 s = new JSlider(JSlider.VERTICAL, 100, 1000, 400);
230
231 s.putClientProperty( "JSlider.isFilled", Boolean.TRUE );
232
233 s.setPaintTicks(true);
234 s.setMajorTickSpacing(100);
235 s.getAccessibleContext().setAccessibleName(getString("SliderDemo.majorticks"));
236 s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.majorticksdescription"));
237 s.addChangeListener(listener);
238 p.add(Box.createRigidArea(HGAP25));
239 p.add(s);
240 p.add(Box.createRigidArea(HGAP25));
241 vp.add(p);
242 vp.add(Box.createRigidArea(HGAP5));
243
244 p = new JPanel();
246 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
247 p.setBorder(new TitledBorder(getString("SliderDemo.minorticks")));
248 s = new JSlider(JSlider.VERTICAL, 0, 100, 60);
249 s.setPaintTicks(true);
250 s.setMajorTickSpacing(20);
251 s.setMinorTickSpacing(5);
252
253 s.setPaintLabels( true );
254
255 s.getAccessibleContext().setAccessibleName(getString("SliderDemo.minorticks"));
256 s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.minorticksdescription"));
257
258 s.addChangeListener(listener);
259 p.add(Box.createRigidArea(HGAP10));
260 p.add(s);
261 p.add(Box.createRigidArea(HGAP10));
262 vp.add(p);
263 vp.add(Box.createRigidArea(HGAP5));
264
265 p = new JPanel();
267 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
268 p.setBorder(new TitledBorder(getString("SliderDemo.disabled")));
269 s = new JSlider(JSlider.VERTICAL, 0, 100, 80);
270 s.setPaintTicks(true);
271 s.setMajorTickSpacing(20);
272 s.setMinorTickSpacing(5);
273 s.setEnabled(false);
274 s.getAccessibleContext().setAccessibleName(getString("SliderDemo.disabled"));
275 s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.disableddescription"));
276 s.addChangeListener(listener);
277 p.add(Box.createRigidArea(HGAP20));
278 p.add(s);
279 p.add(Box.createRigidArea(HGAP20));
280 vp.add(p);
281 }
282
283 class SliderListener implements ChangeListener {
284 JLabel tf;
285 public SliderListener(JLabel f) {
286 tf = f;
287 }
288 public void stateChanged(ChangeEvent e) {
289 JSlider s1 = (JSlider)e.getSource();
290 tf.setText(getString("SliderDemo.slidervalue") + s1.getValue());
291 }
292 }
293 }
294
295