how to change the text of TextField in java fx 2 [closed]

為{幸葍}努か 提交于 2020-01-07 08:54:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!