1   /*
2    * Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    * 
8    * -Redistributions of source code must retain the above copyright
9    *  notice, this list of conditions and the following disclaimer.
10   * 
11   * -Redistribution in binary form must reproduct the above copyright
12   *  notice, this list of conditions and the following disclaimer in
13   *  the documentation and/or other materials provided with the distribution.
14   * 
15   * Neither the name of Sun Microsystems, Inc. or the names of contributors
16   * may be used to endorse or promote products derived from this software
17   * without specific prior written permission.
18   * 
19   * This software is provided "AS IS," without a warranty of any kind. ALL
20   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23   * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24   * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29   * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30   * 
31   * You acknowledge that Software is not designed, licensed or intended for
32   * use in the design, construction, operation or maintenance of any nuclear
33   * facility.
34   */
35  
36  /*
37   * @(#)SliderDemo.java  1.6 03/01/23
38   */
39  
40  
41  import javax.swing.*;
42  import javax.swing.event.*;
43  import javax.swing.text.*;
44  import javax.swing.border.*;
45  import javax.swing.colorchooser.*;
46  import javax.swing.filechooser.*;
47  import javax.accessibility.*;
48  
49  import java.awt.*;
50  import java.awt.event.*;
51  import java.beans.*;
52  import java.util.*;
53  import java.io.*;
54  import java.applet.*;
55  import java.net.*;
56  
57  /**
58   * JSlider Demo
59   *
60   * @version 1.6 01/23/03
61   * @author Dave Kloba
62   * @author Jeff Dinkins
63   */
64  public class SliderDemo extends DemoModule {
65  
66      /**
67       * main method allows us to run as a standalone demo.
68       */
69      public static void main(String[] args) {
70      SliderDemo demo = new SliderDemo(null);
71      demo.mainImpl();
72      }
73  
74      /**
75       * SliderDemo Constructor
76       */
77      public SliderDemo(SwingSet2 swingset) {
78      // Set the title for this demo, and an icon used to represent this
79      // demo inside the SwingSet2 app.
80      super(swingset, "SliderDemo", "toolbar/JSlider.gif");
81  
82      createSliderDemo();
83      }
84  
85      public void createSliderDemo() {
86          JSlider s;
87      JPanel hp;
88      JPanel vp;
89      GridLayout g;
90      JPanel tp;
91      JLabel tf;
92      ChangeListener listener;
93  
94      getDemoPanel().setLayout(new BorderLayout());
95  
96      tf = new JLabel(getString("SliderDemo.slidervalue"));
97      getDemoPanel().add(tf, BorderLayout.SOUTH);
98      
99      tp = new JPanel();
100     g = new GridLayout(1, 2);
101     g.setHgap(5);
102     g.setVgap(5);
103     tp.setLayout(g);
104     getDemoPanel().add(tp, BorderLayout.CENTER);
105         
106     listener = new SliderListener(tf);
107 
108     BevelBorder border = new BevelBorder(BevelBorder.LOWERED);
109 
110     hp = new JPanel();
111     hp.setLayout(new BoxLayout(hp, BoxLayout.Y_AXIS));
112     hp.setBorder(new TitledBorder( 
113             border,
114             getString("SliderDemo.horizontal"),
115             TitledBorder.LEFT,
116             TitledBorder.ABOVE_TOP));
117     tp.add(hp);
118 
119     vp = new JPanel();
120     vp.setLayout(new BoxLayout(vp, BoxLayout.X_AXIS));
121     vp.setBorder(new TitledBorder( 
122             border,
123             getString("SliderDemo.vertical"),
124             TitledBorder.LEFT,
125             TitledBorder.ABOVE_TOP));
126     tp.add(vp);
127 
128     // Horizontal Slider 1
129     JPanel p = new JPanel();
130     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
131     p.setBorder(new TitledBorder(getString("SliderDemo.plain")));
132     s = new JSlider(-10, 100, 20);
133     s.getAccessibleContext().setAccessibleName(getString("SliderDemo.plain"));
134     s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.a_plain_slider"));
135     s.addChangeListener(listener);
136 
137     p.add(Box.createRigidArea(VGAP5));
138     p.add(s);
139     p.add(Box.createRigidArea(VGAP5));
140     hp.add(p);
141     hp.add(Box.createRigidArea(VGAP10));
142 
143     // Horizontal Slider 2
144     p = new JPanel();
145     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
146     p.setBorder(new TitledBorder(getString("SliderDemo.majorticks")));
147     s = new JSlider(100, 1000, 400);
148     s.setPaintTicks(true);
149     s.setMajorTickSpacing(100);
150     s.getAccessibleContext().setAccessibleName(getString("SliderDemo.majorticks"));
151     s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.majorticksdescription"));
152     s.addChangeListener(listener);
153 
154     p.add(Box.createRigidArea(VGAP5));
155     p.add(s);
156     p.add(Box.createRigidArea(VGAP5));
157     hp.add(p);
158     hp.add(Box.createRigidArea(VGAP10));
159 
160     // Horizontal Slider 3
161     p = new JPanel();
162     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
163     p.setBorder(new TitledBorder(getString("SliderDemo.ticks")));
164     s = new JSlider(0, 11, 6);
165 
166     s.putClientProperty("JSlider.isFilled", Boolean.TRUE );
167 
168     s.setPaintTicks(true);
169     s.setMajorTickSpacing(5);
170     s.setMinorTickSpacing(1);
171 
172     s.setPaintLabels( true );
173     s.setSnapToTicks( true );
174 
175     s.getLabelTable().put(new Integer(11), new JLabel(new Integer(11).toString(), JLabel.CENTER));
176     s.setLabelTable( s.getLabelTable() );
177 
178     s.getAccessibleContext().setAccessibleName(getString("SliderDemo.minorticks"));
179     s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.minorticksdescription"));
180 
181     s.addChangeListener(listener);
182 
183     p.add(Box.createRigidArea(VGAP5));
184     p.add(s);
185     p.add(Box.createRigidArea(VGAP5));
186     hp.add(p);
187     hp.add(Box.createRigidArea(VGAP10));
188 
189     // Horizontal Slider 4
190     p = new JPanel();
191     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
192     p.setBorder(new TitledBorder(getString("SliderDemo.disabled")));
193     BoundedRangeModel brm = new DefaultBoundedRangeModel(80, 0, 0, 100);
194     s = new JSlider(brm);
195     s.setPaintTicks(true);
196     s.setMajorTickSpacing(20);
197     s.setMinorTickSpacing(5);
198     s.setEnabled(false);
199     s.getAccessibleContext().setAccessibleName(getString("SliderDemo.disabled"));
200     s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.disableddescription"));
201     s.addChangeListener(listener);
202 
203     p.add(Box.createRigidArea(VGAP5));
204     p.add(s);
205     p.add(Box.createRigidArea(VGAP5));
206     hp.add(p);
207     
208         //////////////////////////////////////////////////////////////////////////////
209 
210     // Vertical Slider 1
211     p = new JPanel();
212     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
213     p.setBorder(new TitledBorder(getString("SliderDemo.plain")));
214     s = new JSlider(JSlider.VERTICAL, -10, 100, 20);
215     s.getAccessibleContext().setAccessibleName(getString("SliderDemo.plain"));
216     s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.a_plain_slider"));
217     s.addChangeListener(listener);
218     p.add(Box.createRigidArea(HGAP10));
219     p.add(s);
220     p.add(Box.createRigidArea(HGAP10));
221     vp.add(p);
222     vp.add(Box.createRigidArea(HGAP10));
223 
224     // Vertical Slider 2
225     p = new JPanel();
226     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
227     p.setBorder(new TitledBorder(getString("SliderDemo.majorticks")));
228     s = new JSlider(JSlider.VERTICAL, 100, 1000, 400);
229 
230     s.putClientProperty( "JSlider.isFilled", Boolean.TRUE );
231 
232     s.setPaintTicks(true);
233     s.setMajorTickSpacing(100);
234     s.getAccessibleContext().setAccessibleName(getString("SliderDemo.majorticks"));
235     s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.majorticksdescription"));
236     s.addChangeListener(listener);
237     p.add(Box.createRigidArea(HGAP25));
238     p.add(s);
239     p.add(Box.createRigidArea(HGAP25));
240     vp.add(p);
241     vp.add(Box.createRigidArea(HGAP5));
242 
243     // Vertical Slider 3
244     p = new JPanel();
245     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
246     p.setBorder(new TitledBorder(getString("SliderDemo.minorticks")));
247     s = new JSlider(JSlider.VERTICAL, 0, 100, 60);
248     s.setPaintTicks(true);
249     s.setMajorTickSpacing(20);
250     s.setMinorTickSpacing(5);
251 
252     s.setPaintLabels( true );
253 
254     s.getAccessibleContext().setAccessibleName(getString("SliderDemo.minorticks"));
255     s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.minorticksdescription"));
256 
257     s.addChangeListener(listener);
258     p.add(Box.createRigidArea(HGAP10));
259     p.add(s);
260     p.add(Box.createRigidArea(HGAP10));
261     vp.add(p);
262     vp.add(Box.createRigidArea(HGAP5));
263 
264     // Vertical Slider 4
265     p = new JPanel();
266     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
267     p.setBorder(new TitledBorder(getString("SliderDemo.disabled")));
268     s = new JSlider(JSlider.VERTICAL, 0, 100, 80);
269     s.setPaintTicks(true);
270     s.setMajorTickSpacing(20);
271     s.setMinorTickSpacing(5);
272     s.setEnabled(false);
273     s.getAccessibleContext().setAccessibleName(getString("SliderDemo.disabled"));
274         s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.disableddescription"));
275     s.addChangeListener(listener);
276     p.add(Box.createRigidArea(HGAP20));
277     p.add(s);
278     p.add(Box.createRigidArea(HGAP20));
279     vp.add(p);
280     }
281 
282     class SliderListener implements ChangeListener {
283     JLabel tf;
284     public SliderListener(JLabel f) {
285         tf = f;
286     }
287     public void stateChanged(ChangeEvent e) {
288         JSlider s1 = (JSlider)e.getSource();
289         tf.setText(getString("SliderDemo.slidervalue") + s1.getValue());
290     }
291     }
292 }
293 
294