Hide input caret of TextField in JavaFX8

前端 未结 2 1127
心在旅途
心在旅途 2020-12-11 17:37

I would like to hide the input caret of a TextField in JavaFX8. I already tried to set the text fill to white (my TextField has a white background), but then my user input a

2条回答
  •  一整个雨季
    2020-12-11 18:13

    I already found a workaround for this, and I want to share this with you of course. If anyone else wants to hide the caret until user input, you can use this:

    I have created a change listener, which changes the text color on user input.

    textfield.textProperty().addListener(new ChangeListener(){
    
            @Override
            public void changed(ObservableValue observable,
                    String oldValue, String newValue) {
    
                if (!textfield.getText().trim().isEmpty()) {
                    textfield.setStyle("-fx-text-fill: black");
                } else {
                    textfield.setStyle("-fx-text-fill: white");
                }
    
            }
    
    });
    

    Make sure the text fill is white on startup, I did this in the css file of my FXML:

    #textfield {
        -fx-text-fill: white;
    }
    

    Sorry for bothering you all with this question. However, this subject is not greatly discussed online, so I hope it could be useful for some of you.

提交回复
热议问题