Gregg1989
Gregg1989

Reputation: 695

Updating screen after a button is pressed

The purpose of this program is to collect surveys from users. Once the user fills out the 3 text fields, he has to click Submit. If all 3 text fields are filled out, a text will show right below the submit button. If any of the 3 fields is blank and the user clicks submit, he will get an error (text).

So, the problem is that the "Success" text and the "Error" text overlap. For example, if I am not able to submit the survey the first time, but get it right the second time, my screen ends up looking like this:

enter image description here

I am using a GridPane for the layout of this scene.

You can see the source code below.

public void handle(ActionEvent event) {
    try {
        FileWriter stream = new FileWriter("out.txt");
        BufferedWriter out = new BufferedWriter(stream);

        Text error = new Text("Error. Make sure all fields are filled out.");
        Text success = new Text("Survey submitted. Thank you!");

        //Save the survey only if all text fields are filled out.
        if (!fullName.getText().contentEquals("") && !email.getText().contentEquals("") && !comment.getText().contentEquals("")) {
              out.write("Name: " + fullName.getText());
              out.write("\tEmail: " + email.getText());
              out.write("\tComment: " + comment.getText());
              out.close();
              success.setFont(new Font("Ariel", 15));
              success.setFill(Color.GREEN);
              grid2.add(success, 0, 13,3,4);
         } else {
              error.setFont(new Font("Ariel", 15));
              error.setFill(Color.RED);
              grid2.add(error, 0, 13,3,4);

         }

     } catch (IOException ex) {
         System.out.println("ERROR SAVING FILE.");
     }
     }
});
grid2.add(submitButton, 0, 11);

Upvotes: 0

Views: 104

Answers (1)

GhostDerfel
GhostDerfel

Reputation: 1551

We find the solution on our chat, the problem was that he was adding the Text Fields on the action handle when the correct way was to update the content on the action handle and have the Text or Label field created with the other elements on the screen :)

Upvotes: 2

Related Questions