1 | package genius.gui.panels;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Dimension;
|
---|
6 |
|
---|
7 | import javax.swing.JLabel;
|
---|
8 | import javax.swing.JPanel;
|
---|
9 | import javax.swing.JSpinner;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Spinner but with text label.
|
---|
13 | *
|
---|
14 | */
|
---|
15 | @SuppressWarnings("serial")
|
---|
16 | public class SpinnerPanel extends JPanel {
|
---|
17 |
|
---|
18 | private final JSpinner spinner;
|
---|
19 | private final NumberModel model;
|
---|
20 | private final JLabel label;
|
---|
21 |
|
---|
22 | public SpinnerPanel(final String labeltext, final NumberModel model) {
|
---|
23 | this.model = model;
|
---|
24 | setLayout(new BorderLayout());
|
---|
25 | label = new JLabel(labeltext);
|
---|
26 | add(label, BorderLayout.WEST);
|
---|
27 | label.setPreferredSize(new Dimension(120, 10));
|
---|
28 |
|
---|
29 | spinner = new JSpinner(model.getSpinnerModel());
|
---|
30 | spinner.setMaximumSize(new Dimension(300, 30));
|
---|
31 | add(spinner, BorderLayout.CENTER);
|
---|
32 | // aligns the RIGHT side of the panel with the center of the parent.
|
---|
33 | // This limits the total width
|
---|
34 | setAlignmentX(Component.RIGHT_ALIGNMENT);
|
---|
35 | setMaximumSize(new Dimension(3000000, 30));
|
---|
36 |
|
---|
37 | model.addListener(data -> enable1());
|
---|
38 | enable1();
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | private void enable1() {
|
---|
43 | boolean enabled = !model.isLocked();
|
---|
44 | spinner.setEnabled(enabled);
|
---|
45 | label.setEnabled(enabled);
|
---|
46 | }
|
---|
47 | }
|
---|