-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDecimalField.java
More file actions
34 lines (28 loc) · 927 Bytes
/
Copy pathDecimalField.java
File metadata and controls
34 lines (28 loc) · 927 Bytes
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
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Toolkit;
import java.text.*;
public class DecimalField extends JTextField {
private NumberFormat format;
public DecimalField(double value, int columns, NumberFormat f) {
super(columns);
setDocument(new FormattedDocument(f));
format = f;
setValue(value);
}
public double getValue() {
double retVal = 0.0;
try {
retVal = format.parse(getText()).doubleValue();
} catch (ParseException e) {
// This should never happen because insertString allows
// only properly formatted data to get in the field.
Toolkit.getDefaultToolkit().beep();
System.err.println("getValue: could not parse: " + getText());
}
return retVal;
}
public void setValue(double value) {
setText(format.format(value));
}
}