J doughe
J doughe

Reputation: 19

How to print text to a text area instead of below it in Java

I made a simple java application. Basically when the user clicks the notify button i want a message to be displayed inside of the text area. Right now the message is displayed underneath the text area and I can't figure out how to change it. Here is my code so far.

    public void start(Stage primaryStage) {

    GridPane root = new GridPane();
    root.setAlignment(Pos.CENTER);
    root.setHgap(10);
    root.setVgap(10);

    Label label = new Label();

    Button btn = new Button();
    btn.setText("Notify");
    btn.setOnAction((ActionEvent event) -> {
        label.setText("You have been notified");
    });            

    Button btn2 = new Button();
    btn2.setText("Clear");
    btn2.setOnAction((ActionEvent event) -> {
        label.setText("");
    });

    TextField textField = new TextField();


    root.add(btn,1,0);
    root.add(label,0,1);
    root.add(btn2,1,1);
    root.add(textField,0,0);

    Scene scene = new Scene(root, 400, 250);
    primaryStage.setTitle("Notifier");
    primaryStage.setScene(scene);
    primaryStage.show();
}

Upvotes: 0

Views: 2938

Answers (1)

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23552

You are setting the text in the label. Set it in the text field:

btn.setOnAction((ActionEvent event) -> {
   textField.setText("You have been notified");
});

Upvotes: 1

Related Questions