source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/gui/drawers/RangeSliderDemo.java @ 84

Revision 84, 5.1 KB checked in by jdalbey, 9 years ago (diff)

RangeSlider?: Fix defect that was allowing slider thumb to be dragged too far right.

Line 
1package scriptbuilder.gui.drawers;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.GridBagConstraints;
6import java.awt.GridBagLayout;
7import java.awt.Insets;
8
9import javax.swing.BorderFactory;
10import javax.swing.JFrame;
11import javax.swing.JLabel;
12import javax.swing.JPanel;
13import javax.swing.SwingUtilities;
14import javax.swing.UIManager;
15import javax.swing.event.ChangeEvent;
16import javax.swing.event.ChangeListener;
17
18/*
19The MIT License
20
21Copyright (c) 2010 Ernest Yu. All rights reserved.
22https://github.com/ernieyu/Swing-range-slider
23
24Permission is hereby granted, free of charge, to any person obtaining a copy
25of this software and associated documentation files (the "Software"), to deal
26in the Software without restriction, including without limitation the rights
27to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28copies of the Software, and to permit persons to whom the Software is
29furnished to do so, subject to the following conditions:
30
31The above copyright notice and this permission notice shall be included in
32all copies or substantial portions of the Software.
33
34THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40THE SOFTWARE.
41 */
42
43/**
44 * Demo application panel to display a range slider.
45 */
46public class RangeSliderDemo extends JPanel
47{
48
49    private JLabel rangeSliderLabel1 = new JLabel();
50    private JLabel rangeSliderValue1 = new JLabel();
51    private JLabel rangeSliderLabel2 = new JLabel();
52    private JLabel rangeSliderValue2 = new JLabel();
53    private RangeSlider rangeSlider = new RangeSlider();
54
55    public RangeSliderDemo()
56    {
57        setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
58        setLayout(new GridBagLayout());
59
60        rangeSliderLabel1.setText("Lower value:");
61        rangeSliderLabel2.setText("Upper value:");
62        rangeSliderValue1.setHorizontalAlignment(JLabel.LEFT);
63        rangeSliderValue2.setHorizontalAlignment(JLabel.LEFT);
64
65        rangeSlider.setPreferredSize(new Dimension(240, rangeSlider.getPreferredSize().height));
66        rangeSlider.setMinimum(0);
67        rangeSlider.setMaximum(20);
68
69        // Add listener to update display.
70        rangeSlider.addChangeListener(new ChangeListener()
71        {
72            public void stateChanged(ChangeEvent e)
73            {
74                RangeSlider slider = (RangeSlider) e.getSource();
75                rangeSliderValue1.setText(String.valueOf(slider.getValue()));
76                rangeSliderValue2.setText(String.valueOf(slider.getUpperValue()));
77            }
78        });
79
80        add(rangeSliderLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
81                GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0));
82        add(rangeSliderValue1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
83                GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 0), 0, 0));
84        add(rangeSliderLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
85                GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0));
86        add(rangeSliderValue2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
87                GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 6, 0), 0, 0));
88        add(rangeSlider, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0,
89                GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
90    }
91
92    public void display()
93    {
94        // Initialize values.
95        rangeSlider.setValue(3);
96        rangeSlider.setUpperValue(7);
97
98        // Initialize value display.
99        rangeSliderValue1.setText(String.valueOf(rangeSlider.getValue()));
100        rangeSliderValue2.setText(String.valueOf(rangeSlider.getUpperValue()));
101
102        // Create window frame.
103        JFrame frame = new JFrame();
104        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
105        frame.setResizable(false);
106        frame.setTitle("Range Slider Demo");
107
108        // Set window content and validate.
109        frame.getContentPane().setLayout(new BorderLayout());
110        frame.getContentPane().add(this, BorderLayout.CENTER);
111        frame.pack();
112
113        // Set window location and display.
114        frame.setLocationRelativeTo(null);
115        frame.setVisible(true);
116    }
117
118    /**
119     * Main application method.
120     *
121     * @param args String[]
122     */
123    public static void main(String[] args)
124    {
125        try
126        {
127            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
128        }
129        catch (Exception ex)
130        {
131            ex.printStackTrace();
132        }
133
134        SwingUtilities.invokeLater(new Runnable()
135        {
136            public void run()
137            {
138                new RangeSliderDemo().display();
139            }
140        });
141    }
142}
Note: See TracBrowser for help on using the repository browser.