0

I have a program that uses one form to add and display an object. I can use the form to add an object fine but when I try to display the same object in the form after this, no text will appear. The object is definitely added correctly and I can print out all of the elements so nothing is null. I think the error is with my .setText() line. I have created a smaller version of the program that just tries to display text:

public class TestApp extends Application {

    private Stage primaryStage;

    @FXML public TextField txtField;
    @FXML public TextArea txtArea;
    @FXML public Button btnTest;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Change Solutions");

        showWelcome();
    }

    public void showWelcome(){
        try{
            AnchorPane page = FXMLLoader.load(TestApp.class.getResource("testFXML.fxml"));
            page.setStyle("-fx-background: #FFFFFF;");
            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Welcome to Change Solutions");
            primaryStage.show();
        }catch (IOException i)
        {
            Logger.getLogger(TestApp.class.getName()).log(Level.SEVERE, null, i);
        }
    }

    public void addText(){
        txtArea.setText("test");
        txtField.setText("test");
        showWelcome();
    }

    public void handleButton(ActionEvent actionEvent) {
        addText();
    }
}

FXML:

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testpack.TestApp">

    <children>
      <AnchorPane layoutX="76.0" layoutY="70.0" prefHeight="243.0" prefWidth="309.0">
         <children>
            <TextField fx:id="txtField" layoutX="45.0" layoutY="40.0" promptText="TextField" AnchorPane.leftAnchor="45.0" AnchorPane.topAnchor="40.0" />
            <TextArea fx:id="txtArea" layoutX="45.0" layoutY="81.0" prefHeight="138.0" prefWidth="149.0" promptText="TextArea" AnchorPane.leftAnchor="45.0" AnchorPane.topAnchor="81.0" />
            <Button fx:id="btnTest" layoutX="217.0" layoutY="109.0" mnemonicParsing="false" onAction="#handleButton" text="Button" AnchorPane.bottomAnchor="109.0" AnchorPane.rightAnchor="40.0" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

The code, both here and in the actual program, compiles without errors. I have see versions of this question before but none of the solutions work for me so I thought I would start a new thread.

fabian
  • 80,457
  • 12
  • 86
  • 114
Nats
  • 11
  • 4
  • You need to load the fxml first before assigning text. Otherwise your program is trying to assign text to nothing. – Eric May 25 '17 at 19:19
  • @Eric The FXML is loaded in `showWelcome()`, which is called from `start()`. – James_D May 25 '17 at 19:20
  • Sorry didn't catch that. Why are you loading it twice? One I start and once in addText? – Eric May 25 '17 at 19:21
  • Your `addText()` method calls `showWelcome()`, which then loads the FXML again and (tries to) put the reloaded FXML in the window. The newly loaded FXML will not have any text in the text field or text area... This part won't work, though, because `primaryStage` will be null in the controller. When I try to run this, I actually see the text "test" in both controls, and then I get a null pointer exception. – James_D May 25 '17 at 19:21
  • It's basically a mistake to use the `Application` class as the controller class. See https://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class – James_D May 25 '17 at 19:23
  • I load the method twice because it used as both a display and an add screen. The methods aren't in the mini program but there is one that takes in and stores the user input and one that pulls the user input back into this screen. I have tried to add the text after calling the stage but it still won't appear for me. – Nats May 26 '17 at 05:23

0 Answers0