我在javafx中使用FXML创建了一个TextField。它链接到一个名为"thesholdBox“的变量(如下图所示,实例化不显示)。我让字段侦听KeyEvents,但是每当我在字段中输入值时,"getText()“总是返回一个空字符串。
Java代码:
@FXML
public void threshBox(KeyEvent e) {
//always empty unless I set it manually using "thresholdBox.setText("TESTING")" or something
String newValue = thresholdBox.getText();
System.out.println("Text: " + newValue);
}FXML:
<TextField fx:id="thresholdBox" layoutX="255.0" layoutY="5.0" onKeyTyped="#threshBox" prefHeight="31.0" prefWidth="91.0" promptText="0.00" />截图示例:

现在,我通常希望thresholdBox.getText()返回“TEST”。测试表明,代码肯定是被触发的,但是每次传递都会给我一个空字符串。有人知道这是为什么吗?我对Javafx相当陌生,虽然我在文档中没有看到任何关于这个问题发生的原因,但我完全有可能遗漏了一些东西。
如果我能把我的问题说得更清楚,请告诉我。
谢谢!
更新:相关代码-
//here are the only two other places thresholdBox is used.
//I cropped irrelevant variable declarations, since they do not interact with thresholdBox.
//class variable
@FXML
TextField thresholdBox;
@Override
public void initialize(URL url, ResourceBundle rb) {
thresholdBox = new TextField();
}发布于 2017-04-05 17:57:55
看起来我不应该在初始化函数中使用"thresholdBox = new ();“,因为它覆盖了我的fxml TextField。
谢谢你,James_D,你的回答。
https://stackoverflow.com/questions/43238048
复制相似问题