| 1 | |
|---|
| 2 | import java.awt.BorderLayout; |
|---|
| 3 | import java.awt.Dimension; |
|---|
| 4 | import java.awt.GridBagConstraints; |
|---|
| 5 | import java.awt.GridBagLayout; |
|---|
| 6 | import java.awt.Insets; |
|---|
| 7 | |
|---|
| 8 | import javax.swing.BorderFactory; |
|---|
| 9 | import javax.swing.JFrame; |
|---|
| 10 | import javax.swing.JLabel; |
|---|
| 11 | import javax.swing.JPanel; |
|---|
| 12 | import javax.swing.SwingUtilities; |
|---|
| 13 | import javax.swing.UIManager; |
|---|
| 14 | import javax.swing.event.ChangeEvent; |
|---|
| 15 | import javax.swing.event.ChangeListener; |
|---|
| 16 | /* |
|---|
| 17 | The MIT License |
|---|
| 18 | |
|---|
| 19 | Copyright (c) 2010 Ernest Yu. All rights reserved. |
|---|
| 20 | |
|---|
| 21 | Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 22 | of this software and associated documentation files (the "Software"), to deal |
|---|
| 23 | in the Software without restriction, including without limitation the rights |
|---|
| 24 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 25 | copies of the Software, and to permit persons to whom the Software is |
|---|
| 26 | furnished to do so, subject to the following conditions: |
|---|
| 27 | |
|---|
| 28 | The above copyright notice and this permission notice shall be included in |
|---|
| 29 | all copies or substantial portions of the Software. |
|---|
| 30 | |
|---|
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 32 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 33 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 34 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 35 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 36 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 37 | THE SOFTWARE. |
|---|
| 38 | */ |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Demo application panel to display a range slider. |
|---|
| 42 | */ |
|---|
| 43 | public 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 | } |
|---|