The code below is fairly simple. I have a simple Person class with name and email address fields and a custom toString() method. Then another simple AddressBook class that holds an ArrayList of Person ojbects. My AddressBook app loads my address book with people and then gets into the gui portion. I simply create a custom ListView and have an add button that takes the user to another scene where the user can enter another Person object. In the handler I add that new person object to the list view and go back to the main scene with the list, and I have the new list with my added person and the same list a few lines down in opposite order. So I end up with twice the amount of Person Objects in my list as duplicates. Someone please have a look and tell me where I am going wrong.
//////////////////////////////////// Person.java /////////////////////////////
public class Person {
private String name;
private String emailAddress;
public Person(String nm, String email) {
name = nm;
emailAddress = email;
}
public String getName() {
return name;
}
public String getEmailAddress() {
return emailAddress;
}
@Override
public String toString() {
String str = name;
return str;
}
}
////////////////////////////////// AddressBook.java /////////////////////////////
import java.util.ArrayList;
public class AddressBook {
private ArrayList<Person> people;
public AddressBook() {
people = new ArrayList<Person>();
}
public void addPerson(Person person) {
people.add(person);
}
public ArrayList<Person> getPeople() {
return people;
}
}
//////////////////////////// AddressBookApp.java //////////////////////////
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.ListView;
import javafx.scene.control.ListCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.util.Callback;
public class AddressBookApp extends Application {
private AddressBook friendsAB;
private ListView<Person> friendsListView;
private Scene listScene;
private Scene addScene;
private TextField nameTextField;
private TextField emailTextField;
public static void main(String[] args) {
launch(args);
}
@Override
public void init() throws FileNotFoundException {
// data looks like =>Tom Jones,tom.jones@gmail.com
// =>Kay Gillis,wonderkay838@yahoo.com
File inputFile = new File("personData.csv");
Scanner inFile = new Scanner(inputFile);
friendsAB = new AddressBook();
while (inFile.hasNextLine()) {
String line = inFile.nextLine();
if (line.equals("")) {
continue;
}
String[] array = line.split(",");
String name = array[0];
String emailAddress = array[1];
Person person = new Person(name,emailAddress);
friendsAB.addPerson(person);
}
}
@Override
public void start(Stage primaryStage) {
// listScene
ArrayList<Person> friendsArrayList = friendsAB.getPeople();
ObservableList<Person> friendsObsvList = FXCollections.observableArrayList(friendsArrayList);
friendsListView = new ListView<Person>(friendsObsvList);
friendsListView.setCellFactory(new Callback<ListView<Person>, ListCell<Person>>() {
@Override
public ListCell<Person> call(ListView<Person> listView) {
return new PersonListViewCell();
}
});
Button addButton = new Button("Add");
AddButtonHandler addHandler = new AddButtonHandler(primaryStage);
addButton.setOnAction(addHandler);
VBox listSceneMainContainer = new VBox(20,friendsListView,addButton);
//
// addScene
Label nameLabel = new Label("Name:");
nameTextField = new TextField();
HBox nameHBox = new HBox(5,nameLabel,nameTextField);
Label emailLabel = new Label("Email Address:");
emailTextField = new TextField();
HBox emailHBox = new HBox(5,emailLabel,emailTextField);
Button changesButton = new Button("Make Changes");
ChangesButtonHandler changesHandler = new ChangesButtonHandler(primaryStage);
changesButton.setOnAction(changesHandler);
VBox changesVBox = new VBox(10,nameHBox,emailHBox,changesButton);
addScene = new Scene(changesVBox,900,900);
//
listScene = new Scene(listSceneMainContainer,900,900);
primaryStage.setTitle("Friends Address Book");
primaryStage.setScene(listScene);
primaryStage.show();
}
public class PersonListViewCell extends ListCell<Person> {
@Override
public void updateItem(Person item, boolean empty) {
super.updateItem(item,empty);
if (item != null) {
setText(item.toString());
}
}
}
public class AddButtonHandler implements EventHandler<ActionEvent> {
private Stage primaryStage;
public AddButtonHandler(Stage primStage) {
primaryStage = primStage;
}
@Override
public void handle(ActionEvent event) {
primaryStage.setScene(addScene);
}
}
public class ChangesButtonHandler implements EventHandler<ActionEvent> {
private Stage primaryStage;
public ChangesButtonHandler(Stage primStage) {
primaryStage = primStage;
}
@Override
public void handle(ActionEvent event) {
Person person = new Person(nameTextField.getText(), emailTextField.getText());
friendsAB.addPerson(person);
friendsListView.getItems().add(person);
nameTextField.clear();
emailTextField.clear();
primaryStage.setScene(listScene);
}
}
}
Any help is much appreciated.