-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTextSlide.java
73 lines (60 loc) · 1.93 KB
/
TextSlide.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.text.NumberFormat;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JSlider;
public class TextSlide extends JPanel {
private final JSlider slider;
private final JFormattedTextField number;
private final double scale;
public TextSlide(double min, double max, double init, int precision) {
super();
scale = Math.pow(10.0, (double)precision);
slider = new JSlider((int)(min * scale), (int)(max * scale), (int)(init * scale));
number = new JFormattedTextField(NumberFormat.getNumberInstance());
number.setValue(init);
setLayout(new GridLayout(1, 2));
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
number.setValue(((Number)slider.getValue()).doubleValue() / scale);
fireStateChanged();
}
});
number.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
slider.setValue((int)(((Number)number.getValue()).doubleValue() * scale));
fireStateChanged();
}
});
add(number);
add(slider);
}
public Double getValue() {
return slider.getValue() / scale;
}
public void setValue(Double val) {
number.setValue(val);
slider.setValue((int)(val * scale));
}
public void addChangeListener(ChangeListener listener) {
listenerList.add(ChangeListener.class, listener);
}
public void removeChangeListener(ChangeListener listener) {
listenerList.remove(ChangeListener.class, listener);
}
protected void fireStateChanged() {
ChangeListener[] listeners = listenerList.getListeners(ChangeListener.class);
if(listeners != null && listeners.length > 0) {
ChangeEvent e = new ChangeEvent(this);
for(ChangeListener listener : listeners) {
listener.stateChanged(e);
}
}
}
}