Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. public class FXNDigitsField extends TextField
  2. {
  3. private long m_digit;
  4. public FXNDigitsField()
  5. {
  6. super();
  7. }
  8. public FXNDigitsField(long number)
  9. {
  10. super();
  11. this.m_digit = number;
  12. onInitialization();
  13. }
  14.  
  15. private void onInitialization()
  16. {
  17. setText(Long.toString(this.m_digit));
  18. }
  19.  
  20. @Override
  21. public void replaceText(int startIndex, int endIndex, String text)
  22. {
  23. if (text.matches(Constants.DIGITS_PATTERN) || text.equals(Constants.EMPTY_STRING)) {
  24. super.replaceText(startIndex, endIndex, text);
  25. }
  26. }
  27.  
  28. @Override
  29. public void replaceSelection(String text)
  30. {
  31. if (text.matches(Constants.DIGITS_PATTERN) || text.equals(Constants.EMPTY_STRING)) {
  32. super.replaceSelection(text);
  33. }
  34. }
  35. }
  36.  
  37. // restrict key input to numerals.
  38. this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
  39. @Override public void handle(KeyEvent keyEvent) {
  40. if (!"0123456789".contains(keyEvent.getCharacter())) {
  41. keyEvent.consume();
  42. }
  43. }
  44. });
  45.  
  46. tyr{
  47. Integer.parseInt(myNumField.getText());
  48. }catch(Exception e){
  49. System.out.println("Non-numeric character exist");
  50. }
  51.  
  52. textField.getTextProperty().addListener(
  53. (observable, oldValue, newValue) -> {
  54. if (newValue.equals(oldValue)) return;
  55. // your validation rules, anything you like
  56. // do whatever you want with newValue
  57. // if value is not valid for your rules
  58. ((StringProperty)observable).setValue(oldValue);
  59. // or if you want to change something in text when
  60. // it is valid for you with some changes that can be automated
  61. // for example change it to upper case
  62. ((StringProperty)observable).setValue(oldValue.toUpperCase());
  63. }
  64. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement