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