KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > DoubleNumberField


1 package sellwin.gui;
2
3 import javax.swing.*;
4 import javax.swing.text.*;
5
6 import java.awt.Toolkit JavaDoc;
7 import java.text.*;
8 import java.util.Locale JavaDoc;
9
10 // SellWin http://sourceforge.net/projects/sellwincrm
11
//Contact support@open-app.com for commercial help with SellWin
12
//This software is provided "AS IS", without a warranty of any kind.
13

14 /**
15  * This class wraps some simple rules to display
16  * double numeric data type in a text field
17  */

18 public class DoubleNumberField extends JTextField {
19     private Toolkit JavaDoc toolkit;
20     private NumberFormat doubleFormatter;
21
22     /**
23      * construct the DoubleNumberField
24      * @param value the double value to render
25      * @param columns the number of columns to display
26      */

27     public DoubleNumberField(double value, int columns) {
28         super(columns);
29         toolkit = Toolkit.getDefaultToolkit();
30         doubleFormatter = DecimalFormat.getNumberInstance(Locale.US);
31         setValue(value);
32     }
33
34     /**
35      * get the double value of this field
36      * @return the value of the field as a double
37      */

38     public final double getValue() {
39         double retVal = 0.00;
40         try {
41             retVal = doubleFormatter.parse(getText()).doubleValue();
42         } catch (ParseException e) {
43             // This should never happen because insertString allows
44
// only properly formatted data to get in the field.
45
toolkit.beep();
46         }
47         return retVal;
48     }
49
50     /**
51      * set the value of this field to a given double
52      * @param value the double to render
53      */

54     public final void setValue(double value) {
55         setText(doubleFormatter.format(value));
56     }
57
58     /**
59      * part of the java spec
60      * @return description
61      */

62     protected final Document createDefaultModel() {
63         return new DoubleNumberDocument();
64     }
65
66     /**
67      * this class is part of the java trickery to render
68      */

69     protected class DoubleNumberDocument extends PlainDocument {
70         public void insertString(int offs,
71                                  String JavaDoc str,
72                                  AttributeSet a)
73                 throws BadLocationException {
74             char[] source = str.toCharArray();
75             char[] result = new char[source.length];
76             int j = 0;
77
78             for (int i = 0; i < result.length; i++) {
79                 if (Character.isDigit(source[i]) || (source[i] == '.'))
80                     result[j++] = source[i];
81                 else {
82                     toolkit.beep();
83                     System.err.println("insertString: " + source[i]);
84                 }
85             }
86             super.insertString(offs, new String JavaDoc(result, 0, j), a);
87         }
88     }
89 }
90
Popular Tags