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         @SuppressWarnings("unchecked")
 168         Dictionary<Object, Object> labelTable = s.getLabelTable();
 169         labelTable.put(Integer.valueOf(11), new JLabel(Integer.valueOf(11).toString(), JLabel.CENTER));
 170         s.setLabelTable( s.getLabelTable() );
 171 
 172         s.getAccessibleContext().setAccessibleName(getString("SliderDemo.minorticks"));
 173         s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.minorticksdescription"));
 174 
 175         s.addChangeListener(listener);
 176 
 177         p.add(Box.createRigidArea(VGAP5));
 178         p.add(s);
 179         p.add(Box.createRigidArea(VGAP5));
 180         hp.add(p);
 181         hp.add(Box.createRigidArea(VGAP10));
 182 
 183         // Horizontal Slider 4
 184         p = new JPanel();
 185         p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
 186         p.setBorder(new TitledBorder(getString("SliderDemo.disabled")));
 187         BoundedRangeModel brm = new DefaultBoundedRangeModel(80, 0, 0, 100);
 188         s = new JSlider(brm);
 189         s.setPaintTicks(true);
 190         s.setMajorTickSpacing(20);
 191         s.setMinorTickSpacing(5);
 192         s.setEnabled(false);
 193         s.getAccessibleContext().setAccessibleName(getString("SliderDemo.disabled"));
 194         s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.disableddescription"));
 195         s.addChangeListener(listener);
 196 
 197         p.add(Box.createRigidArea(VGAP5));
 198         p.add(s);
 199         p.add(Box.createRigidArea(VGAP5));
 200         hp.add(p);
 201 
 202         //////////////////////////////////////////////////////////////////////////////
 203 
 204         // Vertical Slider 1
 205         p = new JPanel();
 206         p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
 207         p.setBorder(new TitledBorder(getString("SliderDemo.plain")));
 208         s = new JSlider(JSlider.VERTICAL, -10, 100, 20);
 209         s.getAccessibleContext().setAccessibleName(getString("SliderDemo.plain"));
 210         s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.a_plain_slider"));
 211         s.addChangeListener(listener);
 212         p.add(Box.createRigidArea(HGAP10));
 213         p.add(s);
 214         p.add(Box.createRigidArea(HGAP10));
 215         vp.add(p);
 216         vp.add(Box.createRigidArea(HGAP10));
 217 
 218         // Vertical Slider 2
 219         p = new JPanel();
 220         p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
 221         p.setBorder(new TitledBorder(getString("SliderDemo.majorticks")));
 222         s = new JSlider(JSlider.VERTICAL, 100, 1000, 400);
 223 
 224         s.putClientProperty( "JSlider.isFilled", Boolean.TRUE );
 225 
 226         s.setPaintTicks(true);
 227         s.setMajorTickSpacing(100);
 228         s.getAccessibleContext().setAccessibleName(getString("SliderDemo.majorticks"));
 229         s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.majorticksdescription"));
 230         s.addChangeListener(listener);
 231         p.add(Box.createRigidArea(HGAP25));
 232         p.add(s);
 233         p.add(Box.createRigidArea(HGAP25));
 234         vp.add(p);
 235         vp.add(Box.createRigidArea(HGAP5));
 236 
 237         // Vertical Slider 3
 238         p = new JPanel();
 239         p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
 240         p.setBorder(new TitledBorder(getString("SliderDemo.minorticks")));
 241         s = new JSlider(JSlider.VERTICAL, 0, 100, 60);
 242         s.setPaintTicks(true);
 243         s.setMajorTickSpacing(20);
 244         s.setMinorTickSpacing(5);
 245 
 246         s.setPaintLabels( true );
 247 
 248         s.getAccessibleContext().setAccessibleName(getString("SliderDemo.minorticks"));
 249         s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.minorticksdescription"));
 250 
 251         s.addChangeListener(listener);
 252         p.add(Box.createRigidArea(HGAP10));
 253         p.add(s);
 254         p.add(Box.createRigidArea(HGAP10));
 255         vp.add(p);
 256         vp.add(Box.createRigidArea(HGAP5));
 257 
 258         // Vertical Slider 4
 259         p = new JPanel();
 260         p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
 261         p.setBorder(new TitledBorder(getString("SliderDemo.disabled")));
 262         s = new JSlider(JSlider.VERTICAL, 0, 100, 80);
 263         s.setPaintTicks(true);
 264         s.setMajorTickSpacing(20);
 265         s.setMinorTickSpacing(5);
 266         s.setEnabled(false);
 267         s.getAccessibleContext().setAccessibleName(getString("SliderDemo.disabled"));
 268         s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.disableddescription"));
 269         s.addChangeListener(listener);
 270         p.add(Box.createRigidArea(HGAP20));
 271         p.add(s);
 272         p.add(Box.createRigidArea(HGAP20));
 273         vp.add(p);
 274     }
 275 
 276     class SliderListener implements ChangeListener {
 277         JLabel tf;
 278         public SliderListener(JLabel f) {
 279             tf = f;
 280         }
 281         public void stateChanged(ChangeEvent e) {
 282             JSlider s1 = (JSlider)e.getSource();
 283             tf.setText(getString("SliderDemo.slidervalue") + s1.getValue());
 284         }
 285     }
 286 }