Java FX 8, trouble setting the value of text field

10,335

You are doing it in the wrong place! If you need to play around with your controls just before your fxml is loaded, you need to do it in the initialize(). For this your controller should implement the Initializable

So your controller becomes :

public class testController implements Initializable{

    @FXML
    private TextField t1;

    public void initialize() {

        System.out.println("hi");

        //You should not re-initialize your textfield
        //t1 = new TextField("j");

        t1.setText("hi");

    }
}
Share:
10,335
simplyblue
Author by

simplyblue

I Hate Programming. I Hate Programming. I Hate Programming. It works! I Love Programming. Java is to JavaScript what Car is to Carpet. XML is like violence - if it's not working for you, you're not using enough of it. It should work!- a programmer's last words. UNIX is simple. It just takes a genius to understand it's simplicity.-- Dennis Ritchie Any fool can write code that a computer can understand. Good programmers write code that humans can understand.-Martin Fowler

Updated on June 24, 2022

Comments

  • simplyblue
    simplyblue almost 2 years

    I'm trying to populate the value of a text field in Java FX.

    I have the Main Class,the controller and the fxml.I have bind the fxml file with controller and the appropriate field in it. When i try to set its value, it fails.

    Main.java

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.FlowPane;
    import javafx.stage.Stage;
    
    
    public class Main extends Application {
    
        private Stage primaryStage;
        private FlowPane rootLayout;
    
        @Override
        public void start(Stage primaryStage) {
            try {
    
    
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(Main.class.getResource("test.fxml"));            
                rootLayout = (FlowPane) loader.load();                      
                Scene scene = new Scene(rootLayout);
                primaryStage.setScene(scene);
                primaryStage.show();
    
    
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    testController.java

    package application;
    
    import javafx.fxml.FXML;
    import javafx.scene.control.TextField;
    
    public class testController {
    
        @FXML
        private TextField t1;
    
        public testController() {
    
            System.out.println("hi");
            t1 = new TextField("j");
            t1.setText("hi");
    
        }
    
    
    
    }
    

    FXML file:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.layout.FlowPane?>
    
    <FlowPane prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.testController">
       <children>
          <TextField fx:id="t1" />
       </children>
    </FlowPane>