Control keyboard input into javafx TextField

后端 未结 6 1759
清酒与你
清酒与你 2020-12-31 17:44

I want to control the input into a Javafx TextField so that I can allow only Numeric input, and so that if the max characters are exceeded, then no change will be made to th

6条回答
  •  盖世英雄少女心
    2020-12-31 17:59

    I have created a customized Textfield that can be added to Java Builder FX (using 'import JAR/FXML file...').

    With this TextField can be set

    1. the characters or numbers allowed
    2. if there is or not the space character
    3. if the input is only CAPITAL (the shown output is in capital)
    4. and the lenght.

    Of course can be improoved, but it's quite usefull. Hope that this will help someone :)

    FX Project LimitedTextField With this project can be created the 'LimitedTextField.jar' file to import in your application or in java builder FX.

    CustomControlExample.java

    package limitedtextfield;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class CustomControlExample extends Application {
        @Override
        public void start(Stage stage) throws Exception {
            LimitedTextField customControl = new LimitedTextField();
            customControl.setText("Hello!");
    
            stage.setScene(new Scene(customControl));
            stage.setTitle("Custom Control");
            stage.setWidth(300);
            stage.setHeight(200);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    custom_control.fxml

    
    
    
    
    
    
        
    
    

    LimitedTextField.java

    package limitedtextfield;
    import javafx.scene.control.TextField;
    
    public class LimitedTextField extends TextField
    {
        private String characters;
        private int max;
        private boolean capital = false;
        private boolean space = true;
    
        static public final String CharactersNumbers = "[qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890èéòàùì ]";
        static public final String Characters = "[qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNMèéòàùì ]";
        static public final String Numbers = "[1234567890 ]";
        static public final String NumbersPoint = "[1234567890. ]";
    
        public LimitedTextField(String l){
            super();
            characters = l;
            max=0;
        }
    
        public LimitedTextField(){
            super();
            characters = "";
            max=0;
        }
    
        public LimitedTextField(String l, int max){
            super();
            characters = l;
            this.max=max;
            //System.out.println("Costruttore");
        }
    
        public LimitedTextField(int max){
            super();
            characters = "";
            this.max=max;
        }
    
        @Override
        public void replaceText(int start, int end, String text)
        {
            if(!characters.equals("")){
                if (validateCh(text))
                {
                    text = check(text);
                    super.replaceText(start, end, text);
                    if(max>0)
                        verifyLengh();
                }
            }else{
                text = check(text);
                super.replaceText(start, end, text);
                if(max>0)
                    verifyLengh();
            }
        }
    
        @Override
        public void replaceSelection(String text)
        {
            if(!characters.equals("")){
                if (validateCh(text))
                {
                    text = check(text);
                    super.replaceSelection(text);
                    if(max>0)
                        verifyLengh();
                }  
            }else{
                text = check(text);
                super.replaceSelection(text);
                if(max>0)
                    verifyLengh();
            }
        }
    
        private boolean validateCh(String text)
        {
            /*
            [abc] Find any of the characters between the brackets 
            [0-9] Find any of the digits between the brackets 
            (x|y) Find any of the alternatives separated with | 
            */
            return ("".equals(text) || text.matches(characters));
        }
    
        private void verifyLengh() {
            if (getText().length() > max) {
                setText(getText().substring(0, max));//use this line if you want to delete the newer characters inserted
                //setText(getText().substring(getText().length()-max, getText().length()));//use this line if you want to delete the older characters inserted
                positionCaret(max);//set the cursor position
            }
    
        }
    
        private String check(String text){
            if(capital)
                text = text.toUpperCase();
            if(!space)
                text = text.replace(" ", "");
    
            return text;
        }
        public void setLimitCharachters(String s){
            this.characters = s;
        }
        public String getLimitCharachters(){
            return characters;
        }
        public void setMaxLenght(int s){
            this.max= s;
        }
        public int getMaxLenght(){
            return max;
        }
        public boolean getCapital(){
            return this.capital;
        }
        public void setCapital(boolean t){
            this.capital = t;
        }
        public boolean getSpace(){
            return this.space;
        }
        public void setSpace(boolean t){
            this.space = t;
        }
    }
    

    Example of use:

    MyFxmlApplication.fxml

    ...
    
    ...
    
          
           
         
         
         
    
    ...
    

    MyFxmlApplicationController.fxml

    ...
    import limitedtextfield.LimitedTextField;
    @FXML
    private LimitedTextField A_Name_S;
    
    ...
     @Override
    public void initialize(URL url, ResourceBundle rb) {
        A_Name_S.setSpace(false);
        A_Name_S.setCapital(true); 
        A_Name_S.setMaxLenght(20);
        A_Name_S.setLimitCharachters(LimitedTextField.Characters);
    }
    

    bye

提交回复
热议问题