eriezelagera

Java - Force jTextField to accept integer

Jun 24th, 2014
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.text.*;
  3.  
  4. /**
  5.  * Force a jTextField to accept only integer value.
  6.  * In order to invoke this to a jTextField,
  7.  * you must customize your jTextField's code, from default-code to custom-creation.
  8.  * Then change the creation of jTextField's instance to the instance of this class (IntegerField).
  9.  *
  10.  * @author Erieze Lagera
  11.  */
  12. public class IntegerField extends JTextField {
  13.  
  14.     private int limit;
  15.     private String field_name;
  16.    
  17.     /**
  18.      * Create a simple instance of IntegerField.
  19.      * @param name Name of jTextField who created this instance
  20.      */
  21.     public IntegerField(String name) {
  22.         super();
  23.         setFieldName(name);
  24.     }
  25.  
  26.     /**
  27.      * Create an instance with column length limitation.
  28.      * This will limit user input length based on your set limit.
  29.      * @param name Name of jTextField who created this instance
  30.      * @param limit Preferred column length
  31.      */
  32.     public IntegerField(String name, int limit) {
  33.         super(limit);
  34.         setLimit(limit);
  35.         setFieldName(name);
  36.         if (limit <= 0) {
  37.             System.out.println("[WARNING] Bad limit value from field " + getFieldName() + ", no limitation will be implemented...");
  38.         }
  39.     }
  40.  
  41.     @Override
  42.     protected Document createDefaultModel() {
  43.         return new UpperCaseDocument();
  44.     }
  45.  
  46.     /**
  47.      * Overrides the document of your jTextField, to perform the hack.
  48.      */
  49.     private class UpperCaseDocument extends PlainDocument {
  50.  
  51.         @Override
  52.         public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
  53.             if (str == null) {
  54.                 return;
  55.             }
  56.  
  57.             char[] charArr = str.toCharArray();
  58.             boolean ok = true;
  59.  
  60.             for (int i = 0; i < charArr.length; i++) {
  61.                 try {
  62.                     Integer.parseInt(String.valueOf(charArr[i]));
  63.                 } catch (NumberFormatException e) {
  64.                     ok = false;
  65.                     break;
  66.                 }
  67.             }
  68.             if (ok) {
  69.                 if (limit > 0) {
  70.                     if (offset < limit) {
  71.                         super.insertString(offset, new String(charArr), a);
  72.                     }
  73.                 }
  74.                 else {
  75.                     super.insertString(offset, new String(charArr), a);
  76.                 }
  77.             }
  78.         }
  79.     }
  80.    
  81.     /**
  82.      * Get the current value for limit.
  83.      * @return The value of <i> limit </i>.
  84.      */
  85.     public int getLimit() {
  86.         return limit;
  87.     }
  88.    
  89.     /**
  90.      * Set the input limit for your jTextField.
  91.      * @param newValue Limit value
  92.      */
  93.     private void setLimit(int newValue) {
  94.         this.limit = newValue;
  95.     }
  96.    
  97.     /**
  98.      * Get the jTextField name who create this instance.
  99.      * @return Name of your jTextField
  100.      */
  101.     public final String getFieldName() {
  102.         return field_name;
  103.     }
  104.  
  105.     /**
  106.      * Set the name of jTextField who create this instance.
  107.      * @param field_name Name of your jTextField
  108.      */
  109.     public void setFieldName(String field_name) {
  110.         this.field_name = field_name;
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment