Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FXNDigitsField extends TextField
- {
- private long m_digit;
- public FXNDigitsField()
- {
- super();
- }
- public FXNDigitsField(long number)
- {
- super();
- this.m_digit = number;
- onInitialization();
- }
- private void onInitialization()
- {
- setText(Long.toString(this.m_digit));
- }
- @Override
- public void replaceText(int startIndex, int endIndex, String text)
- {
- if (text.matches(Constants.DIGITS_PATTERN) || text.equals(Constants.EMPTY_STRING)) {
- super.replaceText(startIndex, endIndex, text);
- }
- }
- @Override
- public void replaceSelection(String text)
- {
- if (text.matches(Constants.DIGITS_PATTERN) || text.equals(Constants.EMPTY_STRING)) {
- super.replaceSelection(text);
- }
- }
- }
- // restrict key input to numerals.
- this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
- @Override public void handle(KeyEvent keyEvent) {
- if (!"0123456789".contains(keyEvent.getCharacter())) {
- keyEvent.consume();
- }
- }
- });
- tyr{
- Integer.parseInt(myNumField.getText());
- }catch(Exception e){
- System.out.println("Non-numeric character exist");
- }
- textField.getTextProperty().addListener(
- (observable, oldValue, newValue) -> {
- if (newValue.equals(oldValue)) return;
- // your validation rules, anything you like
- // do whatever you want with newValue
- // if value is not valid for your rules
- ((StringProperty)observable).setValue(oldValue);
- // or if you want to change something in text when
- // it is valid for you with some changes that can be automated
- // for example change it to upper case
- ((StringProperty)observable).setValue(oldValue.toUpperCase());
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement