Path: blob/master/src/demo/share/jfc/SwingSet2/SliderDemo.java
41149 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.border.*;37import javax.swing.colorchooser.*;38import javax.swing.filechooser.*;39import javax.accessibility.*;4041import java.awt.*;42import java.awt.event.*;43import java.beans.*;44import java.util.*;45import java.io.*;46import java.applet.*;47import java.net.*;4849/**50* JSlider Demo51*52* @author Dave Kloba53* @author Jeff Dinkins54*/55public class SliderDemo extends DemoModule {5657/**58* main method allows us to run as a standalone demo.59*/60public static void main(String[] args) {61SliderDemo demo = new SliderDemo(null);62demo.mainImpl();63}6465/**66* SliderDemo Constructor67*/68public SliderDemo(SwingSet2 swingset) {69// Set the title for this demo, and an icon used to represent this70// demo inside the SwingSet2 app.71super(swingset, "SliderDemo", "toolbar/JSlider.gif");7273createSliderDemo();74}7576public void createSliderDemo() {77JSlider s;78JPanel hp;79JPanel vp;80GridLayout g;81JPanel tp;82JLabel tf;83ChangeListener listener;8485getDemoPanel().setLayout(new BorderLayout());8687tf = new JLabel(getString("SliderDemo.slidervalue"));88getDemoPanel().add(tf, BorderLayout.SOUTH);8990tp = new JPanel();91g = new GridLayout(1, 2);92g.setHgap(5);93g.setVgap(5);94tp.setLayout(g);95getDemoPanel().add(tp, BorderLayout.CENTER);9697listener = new SliderListener(tf);9899BevelBorder border = new BevelBorder(BevelBorder.LOWERED);100101hp = new JPanel();102hp.setLayout(new BoxLayout(hp, BoxLayout.Y_AXIS));103hp.setBorder(new TitledBorder(104border,105getString("SliderDemo.horizontal"),106TitledBorder.LEFT,107TitledBorder.ABOVE_TOP));108tp.add(hp);109110vp = new JPanel();111vp.setLayout(new BoxLayout(vp, BoxLayout.X_AXIS));112vp.setBorder(new TitledBorder(113border,114getString("SliderDemo.vertical"),115TitledBorder.LEFT,116TitledBorder.ABOVE_TOP));117tp.add(vp);118119// Horizontal Slider 1120JPanel p = new JPanel();121p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));122p.setBorder(new TitledBorder(getString("SliderDemo.plain")));123s = new JSlider(-10, 100, 20);124s.getAccessibleContext().setAccessibleName(getString("SliderDemo.plain"));125s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.a_plain_slider"));126s.addChangeListener(listener);127128p.add(Box.createRigidArea(VGAP5));129p.add(s);130p.add(Box.createRigidArea(VGAP5));131hp.add(p);132hp.add(Box.createRigidArea(VGAP10));133134// Horizontal Slider 2135p = new JPanel();136p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));137p.setBorder(new TitledBorder(getString("SliderDemo.majorticks")));138s = new JSlider(100, 1000, 400);139s.setPaintTicks(true);140s.setMajorTickSpacing(100);141s.getAccessibleContext().setAccessibleName(getString("SliderDemo.majorticks"));142s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.majorticksdescription"));143s.addChangeListener(listener);144145p.add(Box.createRigidArea(VGAP5));146p.add(s);147p.add(Box.createRigidArea(VGAP5));148hp.add(p);149hp.add(Box.createRigidArea(VGAP10));150151// Horizontal Slider 3152p = new JPanel();153p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));154p.setBorder(new TitledBorder(getString("SliderDemo.ticks")));155s = new JSlider(0, 11, 6);156157s.putClientProperty("JSlider.isFilled", Boolean.TRUE );158159s.setPaintTicks(true);160s.setMajorTickSpacing(5);161s.setMinorTickSpacing(1);162163s.setPaintLabels( true );164s.setSnapToTicks( true );165166@SuppressWarnings("unchecked")167Dictionary<Object, Object> labelTable = s.getLabelTable();168labelTable.put(Integer.valueOf(11), new JLabel(Integer.valueOf(11).toString(), JLabel.CENTER));169s.setLabelTable( s.getLabelTable() );170171s.getAccessibleContext().setAccessibleName(getString("SliderDemo.minorticks"));172s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.minorticksdescription"));173174s.addChangeListener(listener);175176p.add(Box.createRigidArea(VGAP5));177p.add(s);178p.add(Box.createRigidArea(VGAP5));179hp.add(p);180hp.add(Box.createRigidArea(VGAP10));181182// Horizontal Slider 4183p = new JPanel();184p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));185p.setBorder(new TitledBorder(getString("SliderDemo.disabled")));186BoundedRangeModel brm = new DefaultBoundedRangeModel(80, 0, 0, 100);187s = new JSlider(brm);188s.setPaintTicks(true);189s.setMajorTickSpacing(20);190s.setMinorTickSpacing(5);191s.setEnabled(false);192s.getAccessibleContext().setAccessibleName(getString("SliderDemo.disabled"));193s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.disableddescription"));194s.addChangeListener(listener);195196p.add(Box.createRigidArea(VGAP5));197p.add(s);198p.add(Box.createRigidArea(VGAP5));199hp.add(p);200201//////////////////////////////////////////////////////////////////////////////202203// Vertical Slider 1204p = new JPanel();205p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));206p.setBorder(new TitledBorder(getString("SliderDemo.plain")));207s = new JSlider(JSlider.VERTICAL, -10, 100, 20);208s.getAccessibleContext().setAccessibleName(getString("SliderDemo.plain"));209s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.a_plain_slider"));210s.addChangeListener(listener);211p.add(Box.createRigidArea(HGAP10));212p.add(s);213p.add(Box.createRigidArea(HGAP10));214vp.add(p);215vp.add(Box.createRigidArea(HGAP10));216217// Vertical Slider 2218p = new JPanel();219p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));220p.setBorder(new TitledBorder(getString("SliderDemo.majorticks")));221s = new JSlider(JSlider.VERTICAL, 100, 1000, 400);222223s.putClientProperty( "JSlider.isFilled", Boolean.TRUE );224225s.setPaintTicks(true);226s.setMajorTickSpacing(100);227s.getAccessibleContext().setAccessibleName(getString("SliderDemo.majorticks"));228s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.majorticksdescription"));229s.addChangeListener(listener);230p.add(Box.createRigidArea(HGAP25));231p.add(s);232p.add(Box.createRigidArea(HGAP25));233vp.add(p);234vp.add(Box.createRigidArea(HGAP5));235236// Vertical Slider 3237p = new JPanel();238p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));239p.setBorder(new TitledBorder(getString("SliderDemo.minorticks")));240s = new JSlider(JSlider.VERTICAL, 0, 100, 60);241s.setPaintTicks(true);242s.setMajorTickSpacing(20);243s.setMinorTickSpacing(5);244245s.setPaintLabels( true );246247s.getAccessibleContext().setAccessibleName(getString("SliderDemo.minorticks"));248s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.minorticksdescription"));249250s.addChangeListener(listener);251p.add(Box.createRigidArea(HGAP10));252p.add(s);253p.add(Box.createRigidArea(HGAP10));254vp.add(p);255vp.add(Box.createRigidArea(HGAP5));256257// Vertical Slider 4258p = new JPanel();259p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));260p.setBorder(new TitledBorder(getString("SliderDemo.disabled")));261s = new JSlider(JSlider.VERTICAL, 0, 100, 80);262s.setPaintTicks(true);263s.setMajorTickSpacing(20);264s.setMinorTickSpacing(5);265s.setEnabled(false);266s.getAccessibleContext().setAccessibleName(getString("SliderDemo.disabled"));267s.getAccessibleContext().setAccessibleDescription(getString("SliderDemo.disableddescription"));268s.addChangeListener(listener);269p.add(Box.createRigidArea(HGAP20));270p.add(s);271p.add(Box.createRigidArea(HGAP20));272vp.add(p);273}274275class SliderListener implements ChangeListener {276JLabel tf;277public SliderListener(JLabel f) {278tf = f;279}280public void stateChanged(ChangeEvent e) {281JSlider s1 = (JSlider)e.getSource();282tf.setText(getString("SliderDemo.slidervalue") + s1.getValue());283}284}285}286287288