×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Israel Edet
Added: Jan 4, 2017 10:02 PM
Modified: Jan 4, 2017 10:02 PM
Views: 4
Tags: no tags
Class to format input for fields by Israel Edet
  1.  
  2. package inventorysystem.data;
  3.  
  4. import com.sun.glass.events.KeyEvent;
  5. import static com.sun.javafx.tk.Toolkit.getToolkit;
  6. import java.text.NumberFormat;
  7. import javax.swing.JLabel;
  8. import javax.swing.text.NumberFormatter;
  9.  
  10. /**
  11.  * Class to format input for fields in the application
  12.  * @author Epic
  13.  */
  14. public class NumberFormater {
  15.     public static NumberFormatter integerOnlyField(){
  16.         NumberFormat format = NumberFormat.getCurrencyInstance();
  17.         NumberFormatter formatter = new NumberFormatter(format);
  18.         formatter.setMinimum(0);
  19.         formatter.setMaximum(Integer.MAX_VALUE);
  20.         formatter.setAllowsInvalid(false);
  21.         formatter.setCommitsOnValidEdit(true); // set this to true so that value will be commited on every key stroke instead of on focus lost
  22.         return formatter;
  23.     }
  24.     public static void integerOnlyField(java.awt.event.KeyEvent evt,JLabel errorField){
  25.         char c  = evt.getKeyChar();
  26.         if(!((c >= 0) && (c <='9')) || (c == KeyEvent.VK_BACKSPACE) || (c == KeyEvent.VK_DELETE)){
  27.             errorField.setText("Only text allowed in this field");
  28.             evt.consume();
  29.         }else{
  30.            errorField.setText("");
  31.         }
  32.     }
  33. }
  34.