简体   繁体   English

将 Java FX 表单数据添加到 ArrayList

[英]Add Java FX form data to ArrayList

In school i had the task to create a JavaFX form GUI in which I can write some Strings like name , eMail ...在学校里,我的任务是创建一个 JavaFX 表单GUI ,我可以在其中编写一些字符串,如nameeMail ...

First I should make a class Person in which the data fields are located.首先,我应该创建一个类Person ,其中数据字段位于其中。

And there should be a class AdressDB in which I create a list about Person应该有一个AdressDB类,我在其中创建了一个关于Person的列表

In the class GUI I should create a form that can be used to add "Persons" to the list.在类GUI我应该创建一个可用于将“人员”添加到列表中的表单。 For example with a button.例如使用按钮。

"Create a class AdressDB . This class keeps a list of Person . Person holds typical data fields such as: eMail , Name , .... In the GUI class you implement a form that you can use to add "Persons" to the list." “创建一个类AdressDB此类保持的名单。 PersonPerson拥有典型的数据字段,例如: eMailName ,......在GUI类,你实现一个表单,您可以使用添加‘人’的名单.”

I have already completed the form.我已经完成了表格。 Now my question: How can I add the data from the form to the ArrayList ?现在我的问题是:如何将表单中的数据添加到ArrayList

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
import java.util.ArrayList;
import javafx.scene.text.Text;
import javafx.scene.text.*;

public class GUI extends Application {
    private TextField fieldName, fieldActor;
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Registration Form JavaFX Application");

        GridPane gridPane = createRegistrationFormPane();
        addUIControls(gridPane);
        Scene scene = new Scene(gridPane, 800, 500);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private GridPane createRegistrationFormPane() {
        // Instantiate a new Grid Pane
        GridPane gridPane = new GridPane();

        // Position the pane at the center of the screen, both vertically and horizontally
        gridPane.setAlignment(Pos.CENTER);

        // Set a padding of 20px on each side
        gridPane.setPadding(new Insets(40, 40, 40, 40));

        // Set the horizontal gap between columns
        gridPane.setHgap(10);

        // Set the vertical gap between rows
        gridPane.setVgap(10);

        // Add Column Constraints

        // columnOneConstraints will be applied to all the nodes placed in column one.
        ColumnConstraints columnOneConstraints = new ColumnConstraints(100, 100, Double.MAX_VALUE);
        columnOneConstraints.setHalignment(HPos.RIGHT);

        // columnTwoConstraints will be applied to all the nodes placed in column two.
        ColumnConstraints columnTwoConstrains = new ColumnConstraints(200,200, Double.MAX_VALUE);
        columnTwoConstrains.setHgrow(Priority.ALWAYS);

        gridPane.getColumnConstraints().addAll(columnOneConstraints, columnTwoConstrains);

        return gridPane;
    }

    private void addUIControls(GridPane gridPane) {

        Label headerLabel = new Label("Bitte geben sie Ihre Daten ein");
        headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
        gridPane.add(headerLabel, 0,0,2,1);
        GridPane.setHalignment(headerLabel, HPos.CENTER);
        GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));

        // Add Name Label
        Label nameLabel = new Label("Name : ");
        gridPane.add(nameLabel, 0,1);

        // Add Name Text Field
        TextField name = new TextField();
        name.setPrefHeight(40);
        gridPane.add(name, 1,1);

        // Add Email Label
        Label emailLabel = new Label("E-Mail: ");
        gridPane.add(emailLabel, 0, 2);

        // Add Email Text Field
        TextField email = new TextField();
        email.setPrefHeight(40);
        gridPane.add(email, 1, 2);

        // Add Birthday Label
        Label Geburtsdatum = new Label("Geburtsdatum : ");
        gridPane.add(Geburtsdatum, 0, 3);

        // Add Birhday Field
        TextField geburtsdatum = new TextField();
        geburtsdatum.setPrefHeight(40);
        gridPane.add(geburtsdatum, 1, 3);
        geburtsdatum.setText("Geburtsdatum");


        // Add Submit Button
        Button submitButton = new Button("Submit");
        submitButton.setPrefHeight(40);
        submitButton.setDefaultButton(true);
        submitButton.setPrefWidth(100);
        gridPane.add(submitButton, 0, 4, 2, 1);
        GridPane.setHalignment(submitButton, HPos.CENTER);
        GridPane.setMargin(submitButton, new Insets(20, 0,20,0));





        submitButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {

        ;







                    if(name.getText().isEmpty()) {
                        showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter your name");
                        return;
                    }
                    if(email.getText().isEmpty()) {
                        showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter your email id");
                        return;
                    }
                    if(geburtsdatum.getText().isEmpty()) {
                        showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter a password");
                        return;
                    }

                    showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Wir haben ihre Daten erhalten!", "Vielen dank für ihr Vertrauen " + name.getText());





                }
            });

    }



        private void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
            Alert alert = new Alert(alertType);
            alert.setTitle(title);
            alert.setHeaderText(null);
            alert.setContentText(message);
            alert.initOwner(owner);
            alert.show();
        }





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


}

In the handle method of submitButton.setOnAction you need to read the fields to create a new Person.在 submitButton.setOnAction 的 handle 方法中,您需要读取字段以创建新的 Person。 This new Person than can be added to the ArrayList.这个新的 Person 可以添加到 ArrayList 中。

As long as you implement the buttons setOnAction Method in the same method, where you define the TextFields, this works well.只要您在定义 TextField 的同一方法中实现按钮 setOnAction 方法,就可以很好地工作。 But as soon as you move the buttons setOnAction implementation to another method, you need to define the TextFields as class "variables" (Fields!) to access them.但是,一旦将按钮 setOnAction 实现移动到另一个方法,就需要将 TextFields 定义为类“变量”(字段!)以访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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