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