简体   繁体   中英

Why is not printing user input in NetBeans console? Javafx

I need to print user input in my console (Javafx NetBeans).

This is my code, and strangely is only printing the label name: "Address". Earlier when I only had 2 fields it would only print the last entry, and it would not print the first entry by user when button pressed.

How can I print all the input from user in console?

    package customerentry2;
import javafx.geometry.Insets;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


/**
 *
 * @author 718358
 */

public class CustomerEntry2 extends Application {
    Stage window;
    Scene scene;
    Button button;

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

       @Override
    public void start(Stage primaryStage) throws Exception 
    {
        window = primaryStage;
        window.setTitle("Customer Entry");

        Label nameLabel = new Label("First Name: ");
        Label nameLabel2 = new Label("Last Name: ");
        Label addressInput = new Label("Address: ");
        TextField nameInput = new TextField();
        TextField nameInput2 = new TextField();
        TextField addressInput3 = new TextField();
        button = new Button("Save");
        button.setOnAction(e -> System.out.println(nameInput.getText()));
        button.setOnAction(e -> System.out.println(nameInput2.getText()));
        button.setOnAction(e -> System.out.println(addressInput3.getText()));
        //Layout
        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20, 20, 20, 20));
        layout.getChildren().addAll(nameLabel, nameInput, nameLabel2, nameInput2, addressInput, addressInput3,  button);

        scene = new Scene(layout, 300, 250);
        window.setScene(scene);
        window.show();

    }

    /**
     * @param args the command line arguments
     */


}

The problem is happening because each time you call setOnAction it replaces the existing EventHandler , rather than adding an additional one. There's two ways you can fix this.

You can either take care of all three println s in one EventHandler , like this:

button.setOnAction(e -> {System.out.println(nameInput.getText());
                         System.out.println(nameInput2.getText()));
                         System.out.println(addressInput3.getText());
                        });

Or you can use addEventHandler to add more EventHandler s to the button without replacing the existing ones. That would look like this:

button.addEventHandler(ActionEvent.ACTION,
                       (ActionEvent e) -> System.out.println(nameInput.getText()));
button.addEventHandler(ActionEvent.ACTION,
                       (ActionEvent e) -> System.out.println(nameInput2.getText()));
button.addEventHandler(ActionEvent.ACTION,
                       (ActionEvent e) -> System.out.println(addressInput3.getText()));

Either one should work for you. The first one is a little shorter and easier to read, but the second way is more flexible if you plan to dynamically add and remove the handlers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM