KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SliderDemo


1 /*
2  * @(#)SliderDemo.java 1.9 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)SliderDemo.java 1.9 05/11/17
39  */

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 /**
59  * JSlider Demo
60  *
61  * @version 1.9 11/17/05
62  * @author Dave Kloba
63  * @author Jeff Dinkins
64  */

65 public class SliderDemo extends DemoModule {
66
67     /**
68      * main method allows us to run as a standalone demo.
69      */

70     public static void main(String JavaDoc[] args) {
71     SliderDemo demo = new SliderDemo(null);
72     demo.mainImpl();
73     }
74
75     /**
76      * SliderDemo Constructor
77      */

78     public SliderDemo(SwingSet2 swingset) {
79     // Set the title for this demo, and an icon used to represent this
80
// demo inside the SwingSet2 app.
81
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     // Horizontal Slider 1
130
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     // Horizontal Slider 2
145
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     // Horizontal Slider 3
162
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 JavaDoc(11), new JLabel(new Integer JavaDoc(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     // Horizontal Slider 4
191
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         //////////////////////////////////////////////////////////////////////////////
210

211     // Vertical Slider 1
212
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     // Vertical Slider 2
226
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     // Vertical Slider 3
245
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     // Vertical Slider 4
266
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
Popular Tags