Changes from JavaFX Controller are not saved in List from other Object

173 Views Asked by At

I'm new to Java and JavaFX. As a first project I try to make an adress book but I am getting a problem which I can't resolve.

I have a MainClass which opens a window where the user can select to search a Person or add a Person. I also have a second window which opens when the Person, which the user enters by first- and last name is found and displays the Person's details. This window provides a button which opens another (a third) window where the user can change the details. All the information is stored in a Arzt.java class which contains an ObservableList.

My Problem is that any changes I apply aren't saved in the Arzt.java class ObservableList. I hope you can help me there. Help is much appreciated!

App.java (MainClass):

package com.mycompany.doktorenkartei;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;


    public class App extends Application {
    
        private Scene scene;
        private Stage stage;
        
        
        @Override
        public void start(Stage stage) throws IOException {
            this.stage = stage;
    
            showStartpage();
        }
    
        public void showStartpage() throws IOException {
            try {
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(App.class.getResource("startpage.fxml"));
                
                scene = new Scene(loader.load());
    
                stage.setScene(scene);
                stage.setResizable(false);
                stage.show();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Controller of the first window:

package com.mycompany.doktorenkartei;

import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class StartpageController {
    
    @FXML
    private Button findDocWindow;
    @FXML
    private Button addDocWindow;
    

    @FXML
    private void findDoctorWindow() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("arztSuchen.fxml"));
        Parent root = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setTitle("Suche");
        stage.setScene(new Scene(root));
        stage.show();
    }

    @FXML
    private void addDoctorWindow() {
        System.out.println("Button works also");
    }

}

Controller of the searchwindow:

package com.mycompany.doktorenkartei;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class ArztSuchenController implements Initializable {
    private Kartei kartei;
    
    
    @FXML
    private TextField fieldNachname;

    @FXML
    private TextField fieldVorname;

    @FXML
    private Button searchButton;
    
    @Override
    public void initialize(URL location, ResourceBundle resources){
        kartei = new Kartei();
        
    }
    
    public void setKartei(Kartei kartei){
        this.kartei = kartei;
    }
    
    public ArztSuchenController(){    
    }

    
    @FXML
    void searchForDoctor(ActionEvent event) throws IOException {
        String vorname = fieldVorname.getText();
        String name = fieldNachname.getText();        
        
        if (this.kartei.arztExsist(vorname, name)) {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("arztFenster.fxml"));
            Parent root1 = loader.load();         
            Stage stage = new Stage();
            stage.setScene(new Scene(root1));
            
            ArztFensterController controller = loader.getController();
          
            controller.showInformations(this.kartei.getArzt(vorname, name));
            //closing stage if Arzt found
            Stage stageSuchen = (Stage) searchButton.getScene().getWindow();
            stageSuchen.close();
            
            stage.show();
        } else {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("nichtGefundenFenster.fxml"));
            Parent root1 = loader.load();
            Stage stage = new Stage();
            stage.setScene(new Scene(root1));
            stage.show();
        }

    }

}

Controller of the Window where details are changed:

package com.mycompany.doktorenkartei;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class ArztBearbeitenController {
    
    private Arzt ausgewählterArzt;
    
    @FXML
    private Button speichern;
    
    @FXML
    private TextArea textAnmerkung;

    @FXML
    private TextField textApprobation;

    @FXML
    private TextField textArbeitsbegin;

    @FXML
    private TextField textBerufstätig;

    @FXML
    private TextField textBundesland;

    @FXML
    private TextField textErhaltRate;

    @FXML
    private TextField textFacharzt;

    @FXML
    private TextField textFamilienstand;

    @FXML
    private TextField textGeburtstag;

    @FXML
    private TextField textHeimatadresse;

    @FXML
    private TextField textImpfstatus;

    @FXML
    private TextField textNachname;

    @FXML
    private TextField textName;

    @FXML
    private TextField textNationalität;

    @FXML
    private TextField textSprachkurs;

    @FXML
    private TextField textSprachniveau;

    @FXML
    private TextField textStudium;

    @FXML
    private TextField textUnterkunft;

    @FXML
    private TextField textVisum;
    
    
    public ArztBearbeitenController() {
        
    }
    
    @FXML
    void saveTextfield(ActionEvent event) {
        this.ausgewählterArzt.setVorname(textName.getText());
        this.ausgewählterArzt.setNachname(textNachname.getText());
        this.ausgewählterArzt.setGeburtsdatum(textGeburtstag.getText());
        this.ausgewählterArzt.setNationalität(textNationalität.getText());
        this.ausgewählterArzt.setFamilienstand(textFamilienstand.getText());
        this.ausgewählterArzt.setHeimatadresse(textHeimatadresse.getText());
        this.ausgewählterArzt.setAdresseUnterkunft(textUnterkunft.getText());
        this.ausgewählterArzt.setVisum(textVisum.getText());
        this.ausgewählterArzt.setImpfstatus(textImpfstatus.getText());
        this.ausgewählterArzt.setSprachniveau(textSprachniveau.getText());
        this.ausgewählterArzt.setBerufstätig(textBerufstätig.getText());
        this.ausgewählterArzt.setBundesland(textBundesland.getText());
        this.ausgewählterArzt.setSprachkurs(textSprachkurs.getText());
        this.ausgewählterArzt.setFacharzt(textFacharzt.getText());
        this.ausgewählterArzt.setApprobation(textApprobation.getText());
        this.ausgewählterArzt.setStudium(textStudium.getText());
        this.ausgewählterArzt.setArbeitsbegin(textArbeitsbegin.getText());
        this.ausgewählterArzt.setErhaltRate(textErhaltRate.getText());
        this.ausgewählterArzt.setAnmerkungen(textAnmerkung.getText());
    }

    public void fillTextfields(Arzt arzt) {
        this.ausgewählterArzt = arzt;
        textName.setText(arzt.getVornameString());
        textNachname.setText(arzt.getNachnameString());
        textGeburtstag.setText(arzt.getGeburtsdatumString());
        textNationalität.setText(arzt.getNationalitätString());
        textFamilienstand.setText(arzt.getFamilienstandString());
        textHeimatadresse.setText(arzt.getHeimatadresseString());
        textUnterkunft.setText(arzt.getAdresseUnterkunftString());
        textVisum.setText(arzt.getVisumString());
        textImpfstatus.setText(arzt.getImpfstatusString());
        textSprachniveau.setText(arzt.getSprachniveauString());
        textBerufstätig.setText(arzt.getBerufstätigString());
        textBundesland.setText(arzt.getBundeslandString());
        textSprachkurs.setText(arzt.getSprachkursString());
        textFacharzt.setText(arzt.getFacharztString());
        textApprobation.setText(arzt.getApprobationString());
        textStudium.setText(arzt.getStudiumString());
        textArbeitsbegin.setText(arzt.getArbeitsbeginString());
        textErhaltRate.setText(arzt.getErhaltRateString());
        textAnmerkung.setText(arzt.getAnmerkungenString());
    }
}

The class where the Data should be stored(temporary, in future saved as xml file):

package com.mycompany.doktorenkartei;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;

public class Kartei {

    private ObservableList<Arzt> kartei = FXCollections.observableArrayList();
    @FXML private ArztSuchenController suchenController;
    @FXML private ArztFensterController arztController;
    
    public Kartei() {
        //data for testing
        kartei.add(new Arzt("Nelson", "Mandela"));
        kartei.add(new Arzt("Isaac", "Newton"));
        kartei.add(new Arzt("Josepe", "Trymeout"));
        
    }

    public ObservableList<Arzt> getKartei() {
        return this.kartei;
    }
    
    @FXML private void initialize(){
        suchenController.setKartei(this);
    }
    
    public boolean arztExsist(String vorname, String nachname) {
        for (Arzt e : this.kartei) {
            if (e.getVornameString().equalsIgnoreCase(vorname) && e.getNachnameString().equalsIgnoreCase(nachname)) {
                return true;
            }
        }
        return false;
    }

    public Arzt getArzt(String vorname, String nachname) {
        for (Arzt e : this.kartei) {
            if (e.getVornameString().equalsIgnoreCase(vorname) && e.getNachnameString().equalsIgnoreCase(nachname)) {
                return e;
            }
        }
        return null;
    }
}

The Person Class:

package com.mycompany.doktorenkartei;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Arzt {

    private StringProperty vorname;
    private StringProperty nachname;
    private StringProperty geburtsdatum;
    private StringProperty nationalität;
    private StringProperty familienstand;
    private StringProperty heimatadresse;
    private StringProperty adresseUnterkunft;

    private StringProperty visum;
    private StringProperty impfstatus;
    private StringProperty Sprachniveau;
    private StringProperty berufstätig;

    private StringProperty facharzt;
    private StringProperty approbation;
    private StringProperty studium;

    private StringProperty bundesland;
    private StringProperty sprachkurs;

    private StringProperty arbeitsbegin;
    private StringProperty erhaltRate;
    private StringProperty anmerkungen;

    public Arzt(String vorname, String nachname) {
        this.vorname = new SimpleStringProperty(vorname);
        this.nachname = new SimpleStringProperty(nachname);

        //dummyData
        this.geburtsdatum = new SimpleStringProperty("asdasdasd");
        this.nationalität = new SimpleStringProperty("asdasdasd");
        this.familienstand = new SimpleStringProperty("asdasdasd");
        this.heimatadresse = new SimpleStringProperty("asdasdasd");
        this.adresseUnterkunft = new SimpleStringProperty("asdasdasd");

        this.visum = new SimpleStringProperty("asdasdasd");
        this.impfstatus = new SimpleStringProperty("asdasdasd");
        this.Sprachniveau = new SimpleStringProperty("asdasdasd");
        this.berufstätig = new SimpleStringProperty("asdasdasd");

        this.facharzt = new SimpleStringProperty("asdasdasd");
        this.approbation = new SimpleStringProperty("asdasdasd");
        this.studium = new SimpleStringProperty("asdasdasd");

        this.bundesland = new SimpleStringProperty("asdasdasd");
        this.sprachkurs = new SimpleStringProperty("asdasdasd");

        this.arbeitsbegin = new SimpleStringProperty("asdasdasd");
        this.erhaltRate = new SimpleStringProperty("asdasdasd");
        this.anmerkungen = new SimpleStringProperty("asdasdasd");
    }
// getters and setters
1

There are 1 best solutions below

1
On

Assuming your getters and setters are correct, you probably forgot to set the on-action function of your button. You can do that in your fxml file by adding an "onAction" argument like this:

<Button text="Save" onAction=#saveTextfield/>

Or you can do that in your SceneBuilder like this: 1