Guest User

Untitled

a guest
Dec 11th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. //Option 1) change your JTextField with a JFormattedTextField, like this:
  2.  
  3. try {
  4. MaskFormatter mascara = new MaskFormatter("##.##");
  5. JFormattedTextField textField = new JFormattedTextField(mascara);
  6. textField.setValue(new Float("12.34"));
  7. } catch (Exception e) {
  8. ...
  9. }
  10.  
  11. //Option 2) capture user's input from keyboard, like this:
  12.  
  13. JTextField textField = new JTextField(10);
  14. textField.addKeyListener(new KeyAdapter() {
  15. public void keyTyped(KeyEvent e) {
  16. char c = e.getKeyChar();
  17. if ( ((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
  18. e.consume(); // ignore event
  19. }
  20. }
  21. });
Add Comment
Please, Sign In to add comment