Warning: Can't use blame annotator:
svn blame failed on trunk/src/scriptbuilder/gui/drawers/RangeSliderDemo.java: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 25, 5.0 KB checked in by jdalbey, 9 years ago (diff)

RangeSlider? added to drawers package.

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