I am trying to make a Javafx program that will take the user input from scene one, and show it in a ListView in scene two when a button is pressed. Also, the user can go back to the scene one and add another input, and while in scene two the user can remove one of the inputs inside the listview. I have the following code, but for some reason instead of adding each new input underneath the previous one, it just overwrites the first input. Can you help me figure it out? Thanks!
In scene one I have the following code
public class Controller {
@FXML
private TextField userEmail;
@FXML
public void handleRegisterButton(ActionEvent event) throws IOException{
Data data = Data.getInstance();
data.setEmailAddress(userEmail.getText());
Parent viewEmailsParent = FXMLLoader.load(getClass().getResource("../view/viewUserEmails.fxml"));
Scene viewEmailsScene = new Scene(viewEmailsParent);
Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
window.setScene(viewEmailsScene);
window.show();
}
}
And in scene two, where I'm trying to handle most of it, I have this code:
public class secondController implements Initializable {
ObservableList<String> listOfEmails;
@FXML
ListView<String> emailList = new ListView<>();
@FXML
public void handleBackButton(ActionEvent event) throws IOException {
Parent viewEmailsParent = FXMLLoader.load(getClass().getResource("../view/register.fxml"));
Scene viewEmailsScene = new Scene(viewEmailsParent);
Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
window.setScene(viewEmailsScene);
window.show();
}
@FXML
public void handleDeleteButton(ActionEvent event) throws IOException{
String selected = emailList.getSelectionModel().getSelectedItem();
listOfEmails.remove(selected);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
listOfEmails = FXCollections.observableArrayList(Data.getInstance().getEmailAddress());
emailList.setItems(listOfEmails);
}
}
If it helps, this is the data class
public class Data {
public static Data emailStorage;
private String emailAddress;
private Data(){
}
public static Data getInstance(){
if(emailStorage == null){
emailStorage = new Data();
}
return emailStorage;
}
public String getEmailAddress(){
return emailAddress;
}
public void setEmailAddress(String emailAddress){
this.emailAddress = emailAddress;
}
}