How can a textfield from fxml file be updated by setText in java file?

eckama

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");

I looked at this: how to change the text of TextField in java fx 2

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.

I have also looked at: NullPointerException (JavaFX Label.setText())

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();
        */

    }

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

So I tried:

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

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);
    }  
}
José Pereda

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

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.

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");
    } 
}

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");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Using Gradle, how can I ensure that a file exists at a certain location?

来自分类Dev

How to parse a text file (CSV) into haskell so I can operate on it?

来自分类Dev

How to load bash command history from file

来自分类Dev

How to delete a file from local disk in UWP

来自分类Dev

How to parse values from a JSON file in Python

来自分类Dev

Run batch file from Java code

来自分类Dev

How to remove script initialisation from javaScript file

来自分类Dev

What Is a .vrc file, how is is generated and can you remove it using the IDE?

来自分类Dev

How can I populate Liquibase parameter values from an external properties file?

来自分类Dev

How to read the text file from the Application Directory text file

来自分类Dev

updated xml data not updated in the xml file

来自分类Dev

Where to store properties from properties file in java webapp

来自分类Dev

How to write d.ts file from existing .js file?

来自分类Dev

How can I read float data from a binary file using R

来自分类Dev

Why I can't reach form properties from another file?

来自分类Dev

How can I work with a .csv file using Java Spring Resttample?

来自分类Dev

fxml文件中的文本字段如何由java文件中的setText更新?

来自分类Dev

How can I used ReadAllLines with gzipped file

来自分类Dev

How can i get from a FileInfo[] the file full name full directory including the file extension?

来自分类Dev

How can I print contents of file given filename as stdin in bash?

来自分类Dev

Can't open file from PHP

来自分类Dev

Can i read from .ini file which located in resources files?

来自分类Dev

how to read gcc input from external file

来自分类Dev

Reading and writing Integers from/to file in Java

来自分类Dev

How to erase line from text file in Python?

来自分类Dev

How best to file lock in Java cluster

来自分类Dev

How can I prevent a PDF file from being downloaded or printed with PHP or JavaScript?

来自分类Dev

JavaFx-TextField.setText()引发nullpointerException

来自分类Dev

侦听 FXML 中定义的 TextField 中的更改

Related 相关文章

  1. 1

    Using Gradle, how can I ensure that a file exists at a certain location?

  2. 2

    How to parse a text file (CSV) into haskell so I can operate on it?

  3. 3

    How to load bash command history from file

  4. 4

    How to delete a file from local disk in UWP

  5. 5

    How to parse values from a JSON file in Python

  6. 6

    Run batch file from Java code

  7. 7

    How to remove script initialisation from javaScript file

  8. 8

    What Is a .vrc file, how is is generated and can you remove it using the IDE?

  9. 9

    How can I populate Liquibase parameter values from an external properties file?

  10. 10

    How to read the text file from the Application Directory text file

  11. 11

    updated xml data not updated in the xml file

  12. 12

    Where to store properties from properties file in java webapp

  13. 13

    How to write d.ts file from existing .js file?

  14. 14

    How can I read float data from a binary file using R

  15. 15

    Why I can't reach form properties from another file?

  16. 16

    How can I work with a .csv file using Java Spring Resttample?

  17. 17

    fxml文件中的文本字段如何由java文件中的setText更新?

  18. 18

    How can I used ReadAllLines with gzipped file

  19. 19

    How can i get from a FileInfo[] the file full name full directory including the file extension?

  20. 20

    How can I print contents of file given filename as stdin in bash?

  21. 21

    Can't open file from PHP

  22. 22

    Can i read from .ini file which located in resources files?

  23. 23

    how to read gcc input from external file

  24. 24

    Reading and writing Integers from/to file in Java

  25. 25

    How to erase line from text file in Python?

  26. 26

    How best to file lock in Java cluster

  27. 27

    How can I prevent a PDF file from being downloaded or printed with PHP or JavaScript?

  28. 28

    JavaFx-TextField.setText()引发nullpointerException

  29. 29

    侦听 FXML 中定义的 TextField 中的更改

热门标签

归档