JavaFx expandable Textfield sample

后端 未结 2 878
旧巷少年郎
旧巷少年郎 2021-01-26 10:08

in my company we got a task to implement an autoexpandable textfield. As this functionality is not provided by default we had to develop it from scratch. There are many posibil

相关标签:
2条回答
  • 2021-01-26 10:41

    you can set PrefWidth in the listener of the text field.

    // add listner

    'textField.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            textField.setPrefWidth(textField.getText().length());
        }
    });'
    
    0 讨论(0)
  • 2021-01-26 10:45

    that's work for me fine

     field.setMinWidth(40);
    
        field.textProperty().addListener(new ChangeListener<String>() {
    
         @Override
         public void changed(ObservableValue<? extends String> observable, String oldValue, 
          String newValue) {
             int len = newValue.length();
             field.setPrefWidth(len*10);
           if(newValue.isEmpty() || oldValue.isEmpty()){
                field.setPrefWidth(40);
            }        
         });
    
    0 讨论(0)
提交回复
热议问题