I've created a stage and scene application for java. This program asks the user to click a button that says customer info. Then a dialog pops up in the application window and has Label
s and TextField
s for customer info. Then user clicks ok and it stores the customer info into Person
class object. I am trying to have the user enter the info more than once for different customers and store them into a List
. I am unable to store more than one customer. This is what I have so far:
package application;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
final CustomerInfoPane custInfo = new CustomerInfoPane();
primaryStage.setScene(new Scene(custInfo, 375, 250));
primaryStage.setTitle("Customer Information");
primaryStage.show();
}
public class CustomerInfoPane extends GridPane {
Label firstNameLabel = new Label("First Name: ");
Label lastNameLabel = new Label("Last Name: ");
Label emailLabel = new Label("Email: ");
Label phoneLabel = new Label("Phone Number: ");
TextField enterFirstName = new TextField();
TextField enterLastName = new TextField();
TextField enterEmail = new TextField();
TextField enterPhone = new TextField();
Button enterInfoButton = new Button("Enter your info");
Button okButton = new Button(" OK ");
Button cancelButton = new Button(" Cancel ");
CustomerInfoPane() {
setVgap(10);
setPadding(new Insets(10, 10, 10, 10));
add(enterInfoButton, 0, 0);
enterInfoButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
add(firstNameLabel, 0, 2);
add(lastNameLabel, 0, 3);
add(phoneLabel, 0, 4);
add(emailLabel, 0, 5);
add(enterFirstName, 10, 2);
add(enterLastName, 10, 3);
add(enterPhone, 10, 4);
add(enterEmail, 10, 5);
add(okButton, 0, 7);
add(cancelButton, 10, 7);
enterFirstName.setAlignment(Pos.CENTER_RIGHT);
enterLastName.setAlignment(Pos.CENTER_RIGHT);
enterPhone.setAlignment(Pos.CENTER_RIGHT);
enterEmail.setAlignment(Pos.CENTER_RIGHT);
}
});
okButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String firstName = enterFirstName.getText();
String lastName = enterLastName.getText();
String phoneNum = enterPhone.getText();
String email = enterEmail.getText();
Person person1 = new Person(firstName, lastName, phoneNum, email);
Person person2 = new Person(firstName, lastName, phoneNum, email);
List<Person> people = new ArrayList<Person>();
people.add(person1);
people.add(person2);
for (Person i : people) {
System.out.println(i); //when this prints it print same data twice
}
}
});
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
}
}
public static void main(String[] args) {
launch(args);
}
}
This is my Person Class:
public class Person {
private String firstName;
private String lastName;
private String phoneNum;
private String email;
public Person(String firstName, String lastName, String phoneNum, String email){
this.setFirstName(firstName);
this.setLastName(lastName);
this.setPhoneNum(phoneNum);
this.setEmail(email);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "First Name: " + firstName + "\nLast Name: " + lastName + "\nPhone Number: "
+ phoneNum + "\nEmail: " + email;
}
}
This is a picture of my panel with console output: