Skip to content

Commit aee9058

Browse files
author
BarbDev
committed
oublis
1 parent 19f2200 commit aee9058

File tree

3 files changed

+798
-0
lines changed

3 files changed

+798
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ihm.utils;
2+
3+
import javax.swing.JSlider;
4+
5+
/**
6+
* An extension of JSlider to select a range of values using two thumb controls.
7+
* The thumb controls are used to select the lower and upper value of a range
8+
* with predetermined minimum and maximum values.
9+
*
10+
* <p>Note that RangeSlider makes use of the default BoundedRangeModel, which
11+
* supports an inner range defined by a value and an extent. The upper value
12+
* returned by RangeSlider is simply the lower value plus the extent.</p>
13+
*/
14+
public class RangeSlider extends JSlider {
15+
16+
/**
17+
* Constructs a RangeSlider with default minimum and maximum values of 0
18+
* and 100.
19+
*/
20+
public RangeSlider() {
21+
initSlider();
22+
}
23+
24+
/**
25+
* Constructs a RangeSlider with the specified default minimum and maximum
26+
* values.
27+
*/
28+
public RangeSlider(int min, int max) {
29+
super(min, max);
30+
initSlider();
31+
}
32+
33+
/**
34+
* Initializes the slider by setting default properties.
35+
*/
36+
private void initSlider() {
37+
setOrientation(HORIZONTAL);
38+
}
39+
40+
/**
41+
* Overrides the superclass method to install the UI delegate to draw two
42+
* thumbs.
43+
*/
44+
@Override
45+
public void updateUI() {
46+
setUI(new RangeSliderUI(this));
47+
// Update UI for slider labels. This must be called after updating the
48+
// UI of the slider. Refer to JSlider.updateUI().
49+
updateLabelUIs();
50+
}
51+
52+
/**
53+
* Returns the lower value in the range.
54+
*/
55+
@Override
56+
public int getValue() {
57+
return super.getValue();
58+
}
59+
60+
/**
61+
* Sets the lower value in the range.
62+
*/
63+
@Override
64+
public void setValue(int value) {
65+
int oldValue = getValue();
66+
if (oldValue == value) {
67+
return;
68+
}
69+
70+
// Compute new value and extent to maintain upper value.
71+
int oldExtent = getExtent();
72+
int newValue = Math.min(Math.max(getMinimum(), value), oldValue + oldExtent);
73+
int newExtent = oldExtent + oldValue - newValue;
74+
75+
// Set new value and extent, and fire a single change event.
76+
getModel().setRangeProperties(newValue, newExtent, getMinimum(),
77+
getMaximum(), getValueIsAdjusting());
78+
}
79+
80+
/**
81+
* Returns the upper value in the range.
82+
*/
83+
public int getUpperValue() {
84+
return getValue() + getExtent();
85+
}
86+
87+
/**
88+
* Sets the upper value in the range.
89+
*/
90+
public void setUpperValue(int value) {
91+
// Compute new extent.
92+
int lowerValue = getValue();
93+
int newExtent = Math.min(Math.max(0, value - lowerValue), getMaximum() - lowerValue);
94+
95+
// Set extent to set upper value.
96+
setExtent(newExtent);
97+
}
98+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package ihm.utils;
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+
* Demo application panel to display a range slider.
20+
*/
21+
public class RangeSliderDemo extends JPanel {
22+
23+
private JLabel rangeSliderLabel1 = new JLabel();
24+
private JLabel rangeSliderValue1 = new JLabel();
25+
private JLabel rangeSliderLabel2 = new JLabel();
26+
private JLabel rangeSliderValue2 = new JLabel();
27+
private RangeSlider rangeSlider = new RangeSlider();
28+
29+
public RangeSliderDemo() {
30+
setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
31+
setLayout(new GridBagLayout());
32+
33+
rangeSliderLabel1.setText("Lower value:");
34+
rangeSliderLabel2.setText("Upper value:");
35+
rangeSliderValue1.setHorizontalAlignment(JLabel.LEFT);
36+
rangeSliderValue2.setHorizontalAlignment(JLabel.LEFT);
37+
38+
rangeSlider.setPreferredSize(new Dimension(240, rangeSlider.getPreferredSize().height));
39+
rangeSlider.setMinimum(0);
40+
rangeSlider.setMaximum(10);
41+
42+
// Add listener to update display.
43+
rangeSlider.addChangeListener(new ChangeListener() {
44+
public void stateChanged(ChangeEvent e) {
45+
RangeSlider slider = (RangeSlider) e.getSource();
46+
rangeSliderValue1.setText(String.valueOf(slider.getValue()));
47+
rangeSliderValue2.setText(String.valueOf(slider.getUpperValue()));
48+
}
49+
});
50+
51+
add(rangeSliderLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
52+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0));
53+
add(rangeSliderValue1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
54+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 0), 0, 0));
55+
add(rangeSliderLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
56+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0));
57+
add(rangeSliderValue2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
58+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 6, 0), 0, 0));
59+
add(rangeSlider , new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0,
60+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
61+
}
62+
63+
public void display() {
64+
// Initialize values.
65+
rangeSlider.setValue(3);
66+
rangeSlider.setUpperValue(7);
67+
68+
// Initialize value display.
69+
rangeSliderValue1.setText(String.valueOf(rangeSlider.getValue()));
70+
rangeSliderValue2.setText(String.valueOf(rangeSlider.getUpperValue()));
71+
72+
// Create window frame.
73+
JFrame frame = new JFrame();
74+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
75+
frame.setResizable(false);
76+
frame.setTitle("Range Slider Demo");
77+
78+
// Set window content and validate.
79+
frame.getContentPane().setLayout(new BorderLayout());
80+
frame.getContentPane().add(this, BorderLayout.CENTER);
81+
frame.pack();
82+
83+
// Set window location and display.
84+
frame.setLocationRelativeTo(null);
85+
frame.setVisible(true);
86+
}
87+
88+
/**
89+
* Main application method.
90+
* @param args String[]
91+
*/
92+
public static void main(String[] args) {
93+
try {
94+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
95+
} catch (Exception ex) {
96+
ex.printStackTrace();
97+
}
98+
99+
SwingUtilities.invokeLater(new Runnable() {
100+
public void run() {
101+
new RangeSliderDemo().display();
102+
}
103+
});
104+
}
105+
}

0 commit comments

Comments
 (0)