Back to index

wims  3.65+svn20090927
NumberField.java
Go to the documentation of this file.
00001 /*
00002  * @(#)NumberField.java
00003  * 
00004  * $Id: NumberField.java,v 1.7 2001/05/27 06:13:27 huaz Exp $
00005  * 
00006  * Created Novenmber 25, 2000, 5:13 AM
00007  */
00008 package SharpTools;
00009 import java.awt.*;
00010 import java.awt.event.*;
00011 import javax.swing.*;
00012 import javax.swing.text.*;
00013 
00027 public class NumberField extends JTextField {
00028 
00029     // private Float number;
00030     //    private String oldValue;
00031     //    private Float number;
00032     private boolean positiveOnly;
00033     private boolean integer;
00034 
00040     public NumberField(int columns) {
00041        super(columns);
00042        setDocument(new NumberFilterDocument());
00043        //     setInputVerifier(new NumberVerifier());
00044     }
00045     
00053     public NumberField(int columns, boolean positive) {
00054        this(columns);
00055        this.positiveOnly = positive;
00056        setDocument(new NumberFilterDocument());
00057        //     setInputVerifier(new NumberVerifier());
00058     }
00059 
00068     public NumberField(int columns, boolean positive, boolean integer) {
00069        this(columns);
00070        this.positiveOnly = positive;
00071        this.integer = integer;
00072        setDocument(new NumberFilterDocument());
00073        //     setInputVerifier(new NumberVerifier());
00074     }  
00075 
00083     public NumberField(boolean positive, boolean integer) {
00084        super();
00085        this.positiveOnly = positive;
00086        this.integer = integer;
00087        setDocument(new NumberFilterDocument());
00088        //setInputVerifier(new NumberVerifier());
00089     }
00090     
00096     public Float getNumber() {
00097        try {
00098            return new Float(Float.parseFloat(getText()));
00099        }
00100        catch (Exception e) {
00101            return null;
00102        }
00103     }
00104 
00110     public void setNumber(Float f) {
00111 
00112        if (f == null)
00113            setText(null);
00114        else
00115            setText(f.toString());
00116     }
00117 
00123     public Integer getInteger() {
00124        try {
00125            return new Integer(Integer.parseInt(getText()));
00126        }
00127        catch (Exception e) {
00128            return null;
00129        }
00130     }
00131 
00138     public void setInteger(Integer i) {
00139 
00140        if (i == null)
00141            setText(null);
00142        else
00143            setText(i.toString());
00144     }
00145 
00146     // a class used to filter the user's input
00147     class NumberFilterDocument extends PlainDocument {
00148        private StringBuffer __scratchBuffer;
00149 
00150        public NumberFilterDocument() {
00151            super();
00152            __scratchBuffer = new StringBuffer();
00153        }
00154 
00155        // this is called when user inserts a string into the text field
00156        public void insertString(int offset, String text, AttributeSet aset)
00157            throws BadLocationException {
00158            if (text == null)
00159               return;
00160            
00161            __scratchBuffer.setLength(0);
00162 
00163            // Reject all strings that cause the contents of the field not
00164            // to be a valid number (i.e., string representation of a double)
00165            try {
00166               __scratchBuffer.append(getText(0, getLength()));
00167               __scratchBuffer.insert(offset, text);
00168               // Kludge: Append a 0 so that leading decimal points
00169               // and signs will be accepted
00170               __scratchBuffer.append('0');
00171            } catch(BadLocationException ble) {
00172               ble.printStackTrace();
00173               return;
00174            } catch(StringIndexOutOfBoundsException sioobe) {
00175               sioobe.printStackTrace();
00176               return;
00177            }
00178 
00179            float value;
00180            try {
00181               if (integer)
00182                   value = Integer.parseInt(__scratchBuffer.toString());
00183               else
00184                   value = Float.parseFloat(__scratchBuffer.toString());
00185            } catch(NumberFormatException nfe) {
00186               // Resulting string will not be number, so reject it
00187               return;
00188            }
00189 
00190            if (positiveOnly && value<0)
00191               return;
00192            
00193            super.insertString(offset, text, aset);
00194        }
00195     }
00196 
00197     /*
00198 class NumberVerifier extends InputVerifier {
00199 
00200     NumberVerifier() {
00201     super();
00202     }
00203     
00204     public boolean verify(JComponent input) {
00205        JTextField tf = (JTextField)input;
00206        String number = tf.getText();
00207        float value = 0;
00208        try {
00209            if (integer)
00210               value = Integer.parseInt(number);
00211            else
00212               value = Float.parseFloat(number);
00213        } catch(NumberFormatException nfe) {
00214            // Resulting string will not be number, so reject it
00215            return false;
00216        }
00217 
00218        if (positiveOnly && value < 0)
00219            return false;
00220        
00221        return true;
00222     }
00223 }
00224 */
00225 
00226 
00227 }
00228 
00229 
00230 
00231 
00232 
00233 
00234