How to implement a NumberField in javaFX 2.0?

后端 未结 8 1550
慢半拍i
慢半拍i 2020-12-14 17:54

I need to insert a number field into my UI. So I need to check the key events on a text field in order to check whether the input character is a number. I created a class by

相关标签:
8条回答
  • 2020-12-14 18:28

    Applying the following way it is possible to make TextField into NumberField but this way it will not filter the keyboard input instantly... but if you focus out the field then you will see the result. You may use this for basic numeric input field validation. I am giving here some example... It may help someone :)

    // It will show the number with comma ie. 64,568,455
    TextField numberField = new TextField();
    numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
    
    // It will show the integer number ie. 64568455
    TextField integerField = new TextField();
    integerField.setTextFormatter(new TextFormatter<>(new IntegerStringConverter()));
    
    // It will ensure the float format ie. 2635.0
    TextField floatField = new TextField();
    floatField.setTextFormatter(new TextFormatter<>(new FloatStringConverter()));
    
    0 讨论(0)
  • 2020-12-14 18:31

    Combined BaiJiFeiLong and AJAY PRAKASH solution to support decimal inputs

    package com.mazeworks.cloudhms.view.components;
    
    import javafx.beans.property.BooleanProperty;
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.IntegerPropertyBase;
    import javafx.beans.property.SimpleBooleanProperty;
    import javafx.scene.control.TextField;
    
    public class NumericTextField extends TextField {
    
        /**
         * numericOnly property if set, will allow accept only numeric input.
         */
        private BooleanProperty numericOnly = new SimpleBooleanProperty(this,
                "numericOnly", false);
    
        public final boolean isNumericOnly() {
            return numericOnly.getValue();
        }
    
        public final void setNumericOnly(boolean value) {
            numericOnly.setValue(value);
        }
    
        public final BooleanProperty numericOnlyProperty() {
            return numericOnly;
        }
    
        /**
         * maxSize property, determines the maximum size of the text that 
         can be
         * input.
         */
        public IntegerProperty maxSize = new IntegerPropertyBase(1000) {
    
            @Override
            public String getName() {
                return "maxSize";
            }
    
            @Override
            public Object getBean() {
                return NumericTextField.this;
            }
        };
    
        public final IntegerProperty maxSizeProperty() {
            return maxSize;
        }
    
        ;
    
        public final int getMaxSize() {
            return maxSize.getValue();
        }
    
        public final void setMaxSize(int value) {
            maxSize.setValue(value);
        }
    
        /**
         * this method is called when user inputs text into the textField
         */
        @Override
        public void replaceText(int start, int end, String text) {
            if (numericOnly.getValue() && !text.equals("")) {
                if (!text.matches("^[0-9]*\\.?[0-9]*$")) {
                    return;
                }
            }
            if (getText().length() < getMaxSize() || text.equals("")) {
                super.replaceText(start, end, text);
            }
        }
    
        /**
         * this method is called when user pastes text into the textField
         */
        @Override
        public void replaceSelection(String text) {
            if (numericOnly.getValue() && !text.equals("")) {
                if (!text.matches("^[0-9]*\\.?[0-9]*$")) {
                    return;
                }
            }
            super.replaceSelection(text);
            if (getText().length() > getMaxSize()) {
                String maxSubString = getText().substring(0, getMaxSize());
                setText(maxSubString);
                positionCaret(getMaxSize());
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题