问题
I have a TextField
in which there is some text but I want to change that text on some event but I am getting the NullPointerException
.
I am using setText()
method but still its not working. I am calling that function from other class .
Any Help?
Thanks in advance.
回答1:
At beginning of the controller's class definition:
@FXML private TextField txtDescription;
Within the initialize method, add:
txtDescription = new TextField();
Within a method that acts on that text field, something like:
txtDescription.setText("This is my new text.");
回答2:
Make sure that you have a TextField definition in your .fxml file with the following:
fx:id="myCoolTextField"
If you don't have that, init the text field in your display() method with the following:
myCoolTextField = new TextField();
You may also override the special initialize() method. This method is being called every time your scene updated.
@FXML
public void initialize() {
myCoolTextField.setText("Here is my updated text.");
}
来源:https://stackoverflow.com/questions/10601648/how-to-change-the-text-of-textfield-in-java-fx-2