首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >getText()空?

getText()空?
EN

Stack Overflow用户
提问于 2017-04-05 17:38:33
回答 1查看 3.1K关注 0票数 0

我在javafx中使用FXML创建了一个TextField。它链接到一个名为"thesholdBox“的变量(如下图所示,实例化不显示)。我让字段侦听KeyEvents,但是每当我在字段中输入值时,"getText()“总是返回一个空字符串。

Java代码:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
<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相当陌生,虽然我在文档中没有看到任何关于这个问题发生的原因,但我完全有可能遗漏了一些东西。

如果我能把我的问题说得更清楚,请告诉我。

谢谢!

更新:相关代码-

代码语言:javascript
复制
//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();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-05 17:57:55

看起来我不应该在初始化函数中使用"thresholdBox = new ();“,因为它覆盖了我的fxml TextField。

谢谢你,James_D,你的回答。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43238048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档