Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. MaskFormatter mask = new MaskFormatter("*************"); // Specifies the number of characters allowed.
  2. mask.setValidCharacters("qwertyuiopasdfghjklzxcvbnm" +
  3. " QWERTYUIOPASDFGHJKLZXCVBNM "); // Specifies the valid characters: a-z, A-Z and space.
  4. mask.setPlaceholderCharacter(' '); // If the input is less characters than the mask, the space character will be used to fill the rest. Then you can use the trim method in String to get rid of them.
  5. JFormattedTextField textField = new JFormattedTextField(mask);
  6.  
  7. JTextField textField = new JTextField();
  8. textField.setInputVerifier(new InputVerifier() {
  9. @Override
  10. public boolean verify(JComponent input) {
  11. String text = ((JTextField) input).getText();
  12. if (text.matches("[a-zA-Z ]+")) // Reads: "Any of a-z or A-Z or space one or more times (together, not each)" ---> blank field or field containing anything other than those will return false.
  13. return true;
  14. return false;
  15. }
  16. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement