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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

For loop in FXML file || How to send data from controller to fxml file?

From Dev

For loop in FXML file || How to send data from controller to fxml file?

From Dev

How to use a java variables in FXML file?

From Dev

how to add an object in java lang to the fxml file

From Dev

How to use a java variables in FXML file?

From Dev

How to change label text of .fxml file from another java class(means out side the controller)?

From Dev

How can I combine a FXML file and a group in JavaFX?

From Dev

how can I set the location of a fxml file in javaFX?

From Dev

How to keep Java FXML active while file is loading

From Dev

How to keep Java FXML active while file is loading

From Dev

JavaFX how to style FXML file

From Dev

How to get an imageView in a fxml file

From Dev

Java - How to Open the Last Updated File From a Folder that is Sorted by last modified date?

From Dev

How to setText on TextField from Run() method, using JavaFX Scene Builder?

From Dev

JavaFX can't set WebView in FXML file

From Dev

Scene Builder can't open a FXML file

From Dev

Want access of textfield from another .java file within a program

From Dev

How do I call an object an object from a .fxml file in my controller file?

From Dev

How can I check if my input file is updated?

From Dev

get FXML file nodes using java code

From Dev

get FXML file nodes using java code

From Dev

How can get an instance from .class file in java

From Dev

How can I pull variables from one java file to another?

From Dev

(Java) How can I skip a space when reading from a file?

From Dev

How can I run a ruby file in command line from Java?

From Dev

How can I display image file object from java to jsp?

From Dev

How can I decrypt a password in SFTP file downloading from java

From Dev

How can I get a value from a Java file to a jsp?

From Dev

How to return back file path to updated files to ajax from controller

Related Related

  1. 1

    For loop in FXML file || How to send data from controller to fxml file?

  2. 2

    For loop in FXML file || How to send data from controller to fxml file?

  3. 3

    How to use a java variables in FXML file?

  4. 4

    how to add an object in java lang to the fxml file

  5. 5

    How to use a java variables in FXML file?

  6. 6

    How to change label text of .fxml file from another java class(means out side the controller)?

  7. 7

    How can I combine a FXML file and a group in JavaFX?

  8. 8

    how can I set the location of a fxml file in javaFX?

  9. 9

    How to keep Java FXML active while file is loading

  10. 10

    How to keep Java FXML active while file is loading

  11. 11

    JavaFX how to style FXML file

  12. 12

    How to get an imageView in a fxml file

  13. 13

    Java - How to Open the Last Updated File From a Folder that is Sorted by last modified date?

  14. 14

    How to setText on TextField from Run() method, using JavaFX Scene Builder?

  15. 15

    JavaFX can't set WebView in FXML file

  16. 16

    Scene Builder can't open a FXML file

  17. 17

    Want access of textfield from another .java file within a program

  18. 18

    How do I call an object an object from a .fxml file in my controller file?

  19. 19

    How can I check if my input file is updated?

  20. 20

    get FXML file nodes using java code

  21. 21

    get FXML file nodes using java code

  22. 22

    How can get an instance from .class file in java

  23. 23

    How can I pull variables from one java file to another?

  24. 24

    (Java) How can I skip a space when reading from a file?

  25. 25

    How can I run a ruby file in command line from Java?

  26. 26

    How can I display image file object from java to jsp?

  27. 27

    How can I decrypt a password in SFTP file downloading from java

  28. 28

    How can I get a value from a Java file to a jsp?

  29. 29

    How to return back file path to updated files to ajax from controller

HotTag

Archive