comparison examples/gui/swing/ConversionPanel.java @ 1:5df24c91468d

Oh my what a mess.
author samer
date Fri, 05 Apr 2019 16:26:00 +0100
parents
children
comparison
equal deleted inserted replaced
0:bf79fb79ee13 1:5df24c91468d
1 /*
2 * 1.1+Swing version.
3 */
4
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import java.awt.*;
8 import java.awt.event.*;
9 import java.util.*;
10 import java.text.NumberFormat;
11
12 public class ConversionPanel extends JPanel {
13 DecimalField textField;
14 JComboBox unitChooser;
15 JSlider slider;
16 ConverterRangeModel sliderModel;
17 Converter controller;
18 Unit[] units;
19 String title;
20 final static boolean DEBUG = false;
21 final static boolean COLORS = false;
22 final static int MAX = 10000;
23
24 ConversionPanel(Converter myController, String myTitle,
25 Unit[] myUnits,
26 ConverterRangeModel myModel) {
27 if (COLORS) {
28 setBackground(Color.cyan);
29 }
30 setBorder(BorderFactory.createCompoundBorder(
31 BorderFactory.createTitledBorder(myTitle),
32 BorderFactory.createEmptyBorder(5,5,5,5)));
33
34 //Save arguments in instance variables.
35 controller = myController;
36 units = myUnits;
37 title = myTitle;
38 sliderModel = myModel;
39
40 //Add the text field. It initially displays "0" and needs
41 //to be at least 10 columns wide.
42 NumberFormat numberFormat = NumberFormat.getNumberInstance();
43 numberFormat.setMaximumFractionDigits(2);
44 textField = new DecimalField(0, 10, numberFormat);
45 textField.setValue(sliderModel.getDoubleValue());
46 textField.addActionListener(new ActionListener() {
47 public void actionPerformed(ActionEvent e) {
48 sliderModel.setDoubleValue(textField.getValue());
49 }
50 });
51
52 //Add the combo box.
53 unitChooser = new JComboBox();
54 for (int i = 0; i < units.length; i++) { //Populate it.
55 unitChooser.addItem(units[i].description);
56 }
57 unitChooser.setSelectedIndex(0);
58 sliderModel.setMultiplier(units[0].multiplier);
59 unitChooser.addActionListener(new ActionListener() {
60 public void actionPerformed(ActionEvent e) {
61 //Set new maximums for the sliders.
62 int i = unitChooser.getSelectedIndex();
63 sliderModel.setMultiplier(units[i].multiplier);
64 controller.resetMaxValues(false);
65 }
66 });
67
68 //Add the slider.
69 slider = new JSlider(sliderModel);
70 sliderModel.addChangeListener(new ChangeListener() {
71 public void stateChanged(ChangeEvent e) {
72 textField.setValue(sliderModel.getDoubleValue());
73 }
74 });
75
76 //Make the textfield/slider group a fixed size.
77 JPanel unitGroup = new JPanel() {
78 public Dimension getMinimumSize() {
79 return getPreferredSize();
80 }
81 public Dimension getPreferredSize() {
82 return new Dimension(150,
83 super.getPreferredSize().height);
84 }
85 public Dimension getMaximumSize() {
86 return getPreferredSize();
87 }
88 };
89 if (COLORS) {
90 unitGroup.setBackground(Color.blue);
91 }
92 unitGroup.setBorder(BorderFactory.createEmptyBorder(
93 0,0,0,5));
94 unitGroup.setLayout(new BoxLayout(unitGroup,
95 BoxLayout.Y_AXIS));
96 unitGroup.add(textField);
97 unitGroup.add(slider);
98
99 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
100 add(unitGroup);
101 add(unitChooser);
102 unitGroup.setAlignmentY(TOP_ALIGNMENT);
103 unitChooser.setAlignmentY(TOP_ALIGNMENT);
104 }
105
106 /**
107 * Returns the multiplier (units/meter) for the currently
108 * selected unit of measurement.
109 */
110 public double getMultiplier() {
111 return sliderModel.getMultiplier();
112 }
113
114 public double getValue() {
115 return sliderModel.getDoubleValue();
116 }
117 }