如何通过java文件中的setText更新fxml文件中的文本字段? [英] How can a textfield from fxml file be updated by setText in java file?

查看:256
本文介绍了如何通过java文件中的setText更新fxml文件中的文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望根据某些值更新文本字段中的文本。为了使这个样本更简单,我使我的程序更小。问题似乎是当我把top.setText(这是我的新文本)时;

I am looking to update text in a textfield based on some value. In order to make this sample simpler I have made my program smaller. The problem seems to be when I put top.setText("This is my new Text");

我看了这个:
如何更改java fx 2中TextField的文本

但答案似乎没有意义。我不知道为什么要初始化已经实现的文本字段。无论它不起作用。

but the answer does not seem to make sense. I don't know why you'd initialize a textfield that has already been implemented. Regardless it did not work.

我也看过:
NullPointerException(JavaFX Label.setText())

这似乎是我认为最接近的问题,但是当我做了以下我得到一个错误。为了清楚起见,这是在JavaFXApplication5类中。

This seems to be the closest to what I think is the issue, but when I did the following I get an error. Just for clarity this is in the JavaFXApplication5 class.

try {
        FXMLLoader loader = new FXMLLoader(
            getClass().getResource("FXML.fxml")
        );
        FXMLLoader.setController(this); // non-static method setController(Object) 
                                        // can not be referenced from static context ERROR****
        Parent root = (Parent) loader.load();
        /*Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("java code");
        stage.show();
        */

    }

从互联网上阅读我想知道是否存在竞争条件: http://gotoanswer.stanford.edu/nullpointerexception_in_javafx_initialize_method-8807679/

From reading on the internet I wondered if there was a race condition: http://gotoanswer.stanford.edu/nullpointerexception_in_javafx_initialize_method-8807679/

所以我试过:

Platform.runLater(new Runnable() {
  @Override public void run() {
    top.setText("This is my new Text");
  }
});

但这不起作用。我知道它可以在Scene Builder中设置,但我需要一种方法来根据另一个类的值动态地更改它。如果我能弄清楚如何设置它,我可以弄清楚如何做这个部分。希望这足以得到一些帮助。

But that did not work. I know that it can be set in Scene Builder but I need a way to dynamically change it based on values from another class. I can figure out how to do that part if I can just figure out how to set it to begin with. Hopefully this explains enough to get some help.

    FXMLController Class:
    public class FXMLController implements Initializable {

        @FXML private TextField top;

        public FXMLController() {
            System.out.println("Hi");
            top.setText("This is my new Text"); // This breaks the program *********
        }
        @Override
        public void initialize(URL url, ResourceBundle rb) {
        } 
    }






    FXML.fxml class:
    <?xml version="1.0" encoding="UTF-8"?>

    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.scene.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>

    <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxapplication5.FXMLController">
       <children>
          <TextField fx:id="top" layoutX="171.0" layoutY="68.0" />
       </children>
    </AnchorPane>






JavaFXApplication5 class: // main class
public class JavaFXApplication5 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("java code");
            stage.show();
        }
        catch (Exception ex) {
            Logger.getLogger(JavaFXApplication5.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void main(String[] args) {
        launch(args);
    }  
}


推荐答案

你不能在控制器类( FXMLController )上使用构造函数,因为它将由FXMLLoader启动。

You can't use a constructor on your controller class (FXMLController), since it will be initilized by the FXMLLoader.

你是对的,第一个链接有错误答案,因为文本字段将被初始化(因为 @FXML 注释。

And you are right, the first link has a wrong answer, since the textfield will be initialized (because of the @FXML annotation) in this process.

所以对于初学者,你可以在初始化,因为它将从加载器开始加载, top 将已经实例化。

So for starters, you can add some text to the textfield inside initialize, as it will be loaded from the beginning by the loader, and top will be already instantiated.

public class FXMLController implements Initializable {

    @FXML private TextField top;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        top.setText("This is my first Text");
    } 
}

首先尝试使用您的发布版 JavaFXApplication5 ,并检查是否有效。

Try this first, with your posted version of JavaFXApplication5, and check that works.

有很多方法可以在字段上设置内容,但是如果需要修改另一个类的文本字段,只需添加一个公共方法:

There are many ways to set the content on the field, but if you need to modify the text field from another class, just add a public method for that:

public class FXMLController implements Initializable {

    @FXML private TextField top;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        top.setText("This is my new Text");
    } 

    public void setTopText(String text) {
        // set text from another class
        top.setText(text);
    } 

}

举个例子,你可以得到主要类中的控制器实例,并在显示阶段后使用它将内容传递到文本字段。这将覆盖以前的内容。

As an example, you could get an instance of your controller in your main class, and use it to pass the content to the text field, after the stage is shown. This will override the previous content.

@Override
public void start(Stage stage) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXML.fxml"));
    Parent root = loader.load();
    FXMLController controller = (FXMLController)loader.getController();

    Scene scene = new Scene(root);

    stage.setTitle("Java code");
    stage.setScene(scene);
    stage.show();

    controller.setTopText("New Text");
}

这篇关于如何通过java文件中的setText更新fxml文件中的文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆