2
\$\begingroup\$

I created my first java application for a college assignment. I tried my best to design my application using best practices of MVC design patterns. I have read about it on Stack Overflow and other sites. My question basically is: Do I follow the best practices? Every feedback is much appreciated. Below is my code.

View

I have two views, one for the main view and one for updating the credentials.

Main View

package wolffsoft.addressmanagermvcjavafxview.ui;

wolffsoft.addressmanagermvcjavafx.ui.listeners.MainWindowEventHandlerAddMVCJavaFZView;
import wolffsoft.addressmanagermvcjavafx.ui.listeners.MainWindowEventHandlerClearSearchMVCJavaFXView;
import wolffsoft.addressmanagermvcjavafx.ui.listeners.MainWindowEventHandlerOpenUpdateMVCJavaFXView;
import wolffsoft.addressmanagermvcjavafx.ui.listeners.MainWindowEventHandlerRemoveMVCJavaFXView;
import wolffsoft.addressmanagermvcjavafx.ui.listeners.MainWindowEventHandlerSearchMVCJavaFXView;

public class AddressManagerMVCJavaFXView {
    //private final AddressManagerMVCJavaFXModel model = new AddressManagerMVCJavaFXModel();
    private final AddressManagerMVCUpdateJavaFXView setUpdateV = new AddressManagerMVCUpdateJavaFXView();
    private final Button add = new Button("Add Credentails");
    private final Button search = new Button("Search Credentails");
    private final Button delete = new Button("Delete Credentails");
    private final Button clearSearch = new Button("Clear Credentails");
    private final Button updateView = new Button("Update Credentails");
    private final Label inputFields = new Label("Wolff Address Manager Input Credentials");
    private final Label wolffAddressManager = new Label("Wolff Address Manager Show Credentials");
    private final Label companyNameLabel = new Label("Company Name:");
    private final Label firstNameLabel = new Label("First Name:");
    private final Label lastNameLabel = new Label("Last Name:");
    private final Label emailAddressLabel = new Label("Email Address:");
    private final Label reTypeEmailAddressLabel = new Label("Re-type Email Address:");
    private final Label streetNumberLabel = new Label("Street / Number / Add:");
    private final Label zipcodeCityLabel = new Label("ZipCide / City:");
    private final Label searchEmailAddressLabel = new Label("Email Address:");
    private final Label deleteEmailAddressLabel = new Label("Email Address:");
    private final TextField companyNameTextField = new TextField();
    private final TextField firstNameTextField = new TextField();
    private final TextField lastNameTextField = new TextField();
    private final TextField emailAddressTextField = new TextField();
    private final TextField reTypeEmailAddressTextField = new TextField();
    private final TextField streetTextField = new TextField();
    private final TextField numberTextField = new TextField();
    private final TextField additiveTextField = new TextField();
    private final TextField zipCodeTextField = new TextField();
    private final TextField cityTextField = new TextField();
    private final TextField searchEmailAddressTextField = new TextField();
    private final TextField deleteEmailAddressTextField = new TextField();
    private final TextArea textArea = new TextArea();
    private final Pane rootPane = new Pane();
    private final Pane pane1 = new Pane();
    private final Pane pane2 = new Pane();


    public void start(Stage primaryStage) {
        AddressManagerMVCJavaFXModel model = new AddressManagerMVCJavaFXModel();
        // buttons for pane1
        add.setPrefSize(380, 20);
        add.setLayoutX(10.0);
        add.setLayoutY(240.0);
        add.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        EventHandler addCredentails = new MainWindowEventHandlerAddMVCJavaFZView(this,  companyNameTextField,  firstNameTextField, lastNameTextField, emailAddressTextField, reTypeEmailAddressTextField, streetTextField, numberTextField, additiveTextField, zipCodeTextField, cityTextField);
        add.setOnAction(addCredentails);

        search.setPrefSize(380, 20);
        search.setLayoutX(10.0);
        search.setLayoutY(330.0);
        search.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        EventHandler searchCredentials = new MainWindowEventHandlerSearchMVCJavaFXView(this, searchEmailAddressTextField);
        search.setOnAction(searchCredentials);

        delete.setPrefSize(380, 20);
        delete.setLayoutX(10.0);
        delete.setLayoutY(410.0);
        delete.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        EventHandler deleteCredentials = new MainWindowEventHandlerRemoveMVCJavaFXView(deleteEmailAddressTextField);
        delete.setOnAction(deleteCredentials);

        clearSearch.setPrefSize(380, 20);
        clearSearch.setLayoutX(10.0);
        clearSearch.setLayoutY(470.0);
        clearSearch.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        EventHandler clear = new MainWindowEventHandlerClearSearchMVCJavaFXView(this);
        clearSearch.setOnAction(clear);

        updateView.setPrefSize(380, 20);
        updateView.setLayoutX(10.0);
        updateView.setLayoutY(530.0);
        updateView.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        EventHandler setUpdateView = new MainWindowEventHandlerOpenUpdateMVCJavaFXView(setUpdateV);
        updateView.setOnAction(setUpdateView);

        // Labels for pane1
        companyNameLabel.setPrefSize(100, 30);
        companyNameLabel.setLayoutX(10.0);
        companyNameLabel.setLayoutY(20.0);


        firstNameLabel.setPrefSize(100, 30);
        firstNameLabel.setLayoutX(10.0);
        firstNameLabel.setLayoutY(50.0);

        lastNameLabel.setPrefSize(100, 30);
        lastNameLabel.setLayoutX(10.0);
        lastNameLabel.setLayoutY(80.0);

        emailAddressLabel.setPrefSize(100, 30);
        emailAddressLabel.setLayoutX(10.0);
        emailAddressLabel.setLayoutY(110.0);

        reTypeEmailAddressLabel.setPrefSize(140, 30);
        reTypeEmailAddressLabel.setLayoutX(10.0);
        reTypeEmailAddressLabel.setLayoutY(140.0);

        streetNumberLabel.setPrefSize(140, 30);
        streetNumberLabel.setLayoutX(10.0);
        streetNumberLabel.setLayoutY(170.0);

        zipcodeCityLabel.setPrefSize(100, 30);
        zipcodeCityLabel.setLayoutX(10.0);
        zipcodeCityLabel.setLayoutY(200.0);

        searchEmailAddressLabel.setPrefSize(100, 30);
        searchEmailAddressLabel.setLayoutX(10.0);
        searchEmailAddressLabel.setLayoutY(290.0);

        deleteEmailAddressLabel.setPrefSize(100, 30);
        deleteEmailAddressLabel.setLayoutX(10.0);
        deleteEmailAddressLabel.setLayoutY(370.0);

        // TextFields for pane1
        companyNameTextField.setPrefSize(245, 20);
        companyNameTextField.setLayoutX(145);
        companyNameTextField.setLayoutY(20.0);

        companyNameTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");


        firstNameTextField.setPrefSize(245, 20);
        firstNameTextField.setLayoutX(145.0);
        firstNameTextField.setLayoutY(50.0);
        firstNameTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        lastNameTextField.setPrefSize(245, 20.0);
        lastNameTextField.setLayoutX(145.0);
        lastNameTextField.setLayoutY(80.0);
        lastNameTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        emailAddressTextField.setPrefSize(245, 20.0);
        emailAddressTextField.setLayoutX(145.0);
        emailAddressTextField.setLayoutY(110.0);
        emailAddressTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");


        reTypeEmailAddressTextField.setPrefSize(245, 20);
        reTypeEmailAddressTextField.setLayoutX(145.0);
        reTypeEmailAddressTextField.setLayoutY(140.0);
        reTypeEmailAddressTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        streetTextField.setPrefSize(142.5, 20);
        streetTextField.setLayoutX(145.0);
        streetTextField.setLayoutY(170.0);
        streetTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        numberTextField.setPrefSize(43, 20);
        numberTextField.setLayoutX(297.5);
        numberTextField.setLayoutY(170.0);
        numberTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        additiveTextField.setPrefSize(40, 20);
        additiveTextField.setLayoutX(350.5);
        additiveTextField.setLayoutY(170.0);
        additiveTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        zipCodeTextField.setPrefSize(117.5, 20);
        zipCodeTextField.setLayoutX(145.0);
        zipCodeTextField.setLayoutY(200.0);
        zipCodeTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        cityTextField.setPrefSize(117.5, 20);
        cityTextField.setLayoutX(272.5);
        cityTextField.setLayoutY(200.0);
        cityTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        searchEmailAddressTextField.setPrefSize(245, 20);
        searchEmailAddressTextField.setLayoutX(145.0);
        searchEmailAddressTextField.setLayoutY(290.0);
        searchEmailAddressTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        deleteEmailAddressTextField.setPrefSize(245, 20);
        deleteEmailAddressTextField.setLayoutX(145.0);
        deleteEmailAddressTextField.setLayoutY(370.0);
        deleteEmailAddressTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        // set pane1 lay-out
        pane1.setLayoutX(10.0);
        pane1.setLayoutY(20.0);
        pane1.setPrefSize(400, 635);
        pane1.setPadding(new Insets(20.0, 20.0, 20.0, 20.0));
        pane1.setStyle("-fx-background-color: lightgrey; -fx-background-radius: 10px; -fx-effect: innershadow(two-pass-box, gray, 5, 0.5, 1, 1);");

        // add Labels to pane1
        pane1.getChildren().add(companyNameLabel);
        pane1.getChildren().add(firstNameLabel);
        pane1.getChildren().add(lastNameLabel);
        pane1.getChildren().add(emailAddressLabel);
        pane1.getChildren().add(reTypeEmailAddressLabel);
        pane1.getChildren().add(streetNumberLabel);
        pane1.getChildren().add(zipcodeCityLabel);
        pane1.getChildren().add(add);
        pane1.getChildren().add(searchEmailAddressLabel);
        pane1.getChildren().add(search);
        pane1.getChildren().add(deleteEmailAddressLabel);
        pane1.getChildren().add(delete);
        pane1.getChildren().add(clearSearch);
        pane1.getChildren().add(updateView);

        // add TextFields to pane1
        pane1.getChildren().add(companyNameTextField);
        pane1.getChildren().add(firstNameTextField);
        pane1.getChildren().add(lastNameTextField);
        pane1.getChildren().add(emailAddressTextField);
        pane1.getChildren().add(reTypeEmailAddressTextField);
        pane1.getChildren().add(streetTextField);
        pane1.getChildren().add(numberTextField);
        pane1.getChildren().add(additiveTextField);
        pane1.getChildren().add(zipCodeTextField);
        pane1.getChildren().add(cityTextField);
        pane1.getChildren().add(searchEmailAddressTextField);
        pane1.getChildren().add(deleteEmailAddressTextField);

        // set Pane for adding TextArea
        pane2.setLayoutX(430.0);
        pane2.setLayoutY(20.0);
        pane2.setPrefSize(360, 635);
        pane2.setStyle("-fx-background-color: lightgrey; -fx-background-radius: 10px; -fx-effect: innershadow(two-pass-box, gray, 5, 0.5, 1, 1);");

        textArea.setLayoutX(10.0);
        textArea.setLayoutY(15.0);
        textArea.setPrefSize(340, 605);
        textArea.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        pane2.getChildren().add(textArea);

        //set label on rootPane for pane1, pane2
        inputFields.setLayoutX(10.0);
        inputFields.setLayoutY(0.0);

        wolffAddressManager.setLayoutX(430.0);
        wolffAddressManager.setLayoutY(0.0);

        // rootPane for adding 2 panes
        rootPane.setStyle("-fx-background-color: lightgrey;");

        // add panes, nodes to rootPane
        rootPane.getChildren().addAll(pane1, pane2, wolffAddressManager, inputFields);
        model.setRootPane(rootPane);
        model.setScene(new Scene(model.getRootPane(), 800, 675));
        rootPane.getChildren().removeAll();
        primaryStage.setTitle("Wolff Address Manager");
        primaryStage.setScene(model.getScene());
        primaryStage.show();

    }

    public void setTextArea(String text) {
        textArea.setText(text);
    }


    public String getTextArea() {
        return textArea.getText();
    }
}

Update Credentials View

package wolffsoft.addressmanagermvcjavafxview.ui;
public class AddressManagerMVCUpdateJavaFXView extends Stage {


    private final AddressManagerMVCJavaFXModel model = new AddressManagerMVCJavaFXModel();

    private final Button clearTextArea = new Button("Clear TextView");
    private final Button updateCredentails = new Button("Update Credentails");
    private final Label inputFields = new Label("Wolff Address Manager Input Credentials");
    private final Label wolffAddressManager = new Label("Wolff Address Manager Show Credentials");
    private final Label emailCheckUpdateLabel = new Label("Email for Update:");
    private final Label companyNameLabel = new Label("Company Name:");
    private final Label firstNameLabel = new Label("First Name:");
    private final Label lastNameLabel = new Label("Last Name:");
    private final Label emailAddressLabel = new Label("Email Address:");
    private final Label streetNumberLabel = new Label("Street / Number / Add:");
    private final Label zipcodeCityLabel = new Label("ZipCide / City:");
    private final TextField emailCheckUpdateTextField = new TextField();
    private final TextField companyNameTextField = new TextField();
    private final TextField firstNameTextField = new TextField();
    private final TextField lastNameTextField = new TextField();
    private final TextField emailAddressTextField = new TextField();
    private final TextField streetTextField = new TextField();
    private final TextField numberTextField = new TextField();
    private final TextField additiveTextField = new TextField();
    private final TextField zipCodeTextField = new TextField();
    private final TextField cityTextField = new TextField();
    private final TextArea textArea = new TextArea();
    private final Pane paneUpdate = new Pane();
    private final Pane paneUpdate1 = new Pane();
    private final Pane paneUpdate2 = new Pane();

    public Scene createScene() {
        // buttons for paneUpdate1
        updateCredentails.setPrefSize(380, 20);
        updateCredentails.setLayoutX(10.0);
        updateCredentails.setLayoutY(250.0);
        updateCredentails.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        EventHandler updateCredentials = new UpdateWindowEventHandlerUpdateMVCJavaFXView(this, emailCheckUpdateTextField, companyNameTextField, firstNameTextField, lastNameTextField,  emailAddressTextField, streetTextField, numberTextField, additiveTextField, zipCodeTextField, cityTextField);
        updateCredentails.setOnAction(updateCredentials);

        clearTextArea.setPrefSize(380, 20);
        clearTextArea.setLayoutX(10.0);
        clearTextArea.setLayoutY(290.0);
        clearTextArea.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        EventHandler clear = new UpdateViewEventHandlerClearSearchMVCJavaFXView(this);
        clearTextArea.setOnAction(clear);

        // Labels paneUpdate1
        emailCheckUpdateLabel.setPrefSize(140, 30);
        emailCheckUpdateLabel.setLayoutX(10.0);
        emailCheckUpdateLabel.setLayoutY(20.0);

        companyNameLabel.setPrefSize(100, 30);
        companyNameLabel.setLayoutX(10.0);
        companyNameLabel.setLayoutY(50.0);

        firstNameLabel.setPrefSize(100, 30);
        firstNameLabel.setLayoutX(10.0);
        firstNameLabel.setLayoutY(80.0);

        lastNameLabel.setPrefSize(100, 30);
        lastNameLabel.setLayoutX(10.0);
        lastNameLabel.setLayoutY(110.0);

        emailAddressLabel.setPrefSize(100, 30);
        emailAddressLabel.setLayoutX(10.0);
        emailAddressLabel.setLayoutY(140.0);

        streetNumberLabel.setPrefSize(140, 30);
        streetNumberLabel.setLayoutX(10.0);
        streetNumberLabel.setLayoutY(170.0);

        zipcodeCityLabel.setPrefSize(100, 30);
        zipcodeCityLabel.setLayoutX(10.0);
        zipcodeCityLabel.setLayoutY(200.0);

        // TextFields paneUpdate1
        emailCheckUpdateTextField.setPrefSize(245, 20);
        emailCheckUpdateTextField.setLayoutX(145.0);
        emailCheckUpdateTextField.setLayoutY(20.0);
        emailCheckUpdateTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        companyNameTextField.setPrefSize(245, 20);
        companyNameTextField.setLayoutX(145);
        companyNameTextField.setLayoutY(50.0);
        companyNameTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");


        firstNameTextField.setPrefSize(245, 20);
        firstNameTextField.setLayoutX(145.0);
        firstNameTextField.setLayoutY(80.0);
        firstNameTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        lastNameTextField.setPrefSize(245, 20.0);
        lastNameTextField.setLayoutX(145.0);
        lastNameTextField.setLayoutY(110.0);
        lastNameTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        emailAddressTextField.setPrefSize(245, 20.0);
        emailAddressTextField.setLayoutX(145.0);
        emailAddressTextField.setLayoutY(140.0);
        emailAddressTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        streetTextField.setPrefSize(142.5, 20);
        streetTextField.setLayoutX(145.0);
        streetTextField.setLayoutY(170.0);
        streetTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        numberTextField.setPrefSize(43, 20);
        numberTextField.setLayoutX(297.5);
        numberTextField.setLayoutY(170.0);
        numberTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        additiveTextField.setPrefSize(40, 20);
        additiveTextField.setLayoutX(350.5);
        additiveTextField.setLayoutY(170.0);
        additiveTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        zipCodeTextField.setPrefSize(117.5, 20);
        zipCodeTextField.setLayoutX(145.0);
        zipCodeTextField.setLayoutY(200.0);
        zipCodeTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        cityTextField.setPrefSize(117.5, 20);
        cityTextField.setLayoutX(272.5);
        cityTextField.setLayoutY(200.0);
        cityTextField.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");

        // set paneUpdate1 lay-out
        paneUpdate1.setLayoutX(10.0);
        paneUpdate1.setLayoutY(20.0);
        paneUpdate1.setPrefSize(400, 635);
        paneUpdate1.setPadding(new Insets(20.0, 20.0, 20.0, 20.0));
        paneUpdate1.setStyle("-fx-background-color: lightgrey; -fx-background-radius: 10px; -fx-effect: innershadow(two-pass-box, gray, 5, 0.5, 1, 1);");

        // add Buttons To paneUpdate1
        paneUpdate1.getChildren().add(updateCredentails);
        paneUpdate1.getChildren().add(clearTextArea);

        // add Labels to paneUpdate1
        paneUpdate1.getChildren().add(emailCheckUpdateLabel);
        paneUpdate1.getChildren().add(companyNameLabel);
        paneUpdate1.getChildren().add(firstNameLabel);
        paneUpdate1.getChildren().add(lastNameLabel);
        paneUpdate1.getChildren().add(emailAddressLabel);
        paneUpdate1.getChildren().add(streetNumberLabel);
        paneUpdate1.getChildren().add(zipcodeCityLabel);


        // add TextFields to pane1
        paneUpdate1.getChildren().add(emailCheckUpdateTextField);
        paneUpdate1.getChildren().add(companyNameTextField);
        paneUpdate1.getChildren().add(firstNameTextField);
        paneUpdate1.getChildren().add(lastNameTextField);
        paneUpdate1.getChildren().add(emailAddressTextField);
        paneUpdate1.getChildren().add(streetTextField);
        paneUpdate1.getChildren().add(numberTextField);
        paneUpdate1.getChildren().add(additiveTextField);
        paneUpdate1.getChildren().add(zipCodeTextField);
        paneUpdate1.getChildren().add(cityTextField);

        // set Pane for adding TextArea
        paneUpdate2.setLayoutX(430.0);
        paneUpdate2.setLayoutY(20.0);
        paneUpdate2.setPrefSize(360, 635);
        paneUpdate2.setStyle("-fx-background-color: lightgrey; -fx-background-radius: 10px; -fx-effect: innershadow(two-pass-box, gray, 5, 0.5, 1, 1);");

        textArea.setLayoutX(10.0);
        textArea.setLayoutY(15.0);
        textArea.setPrefSize(340, 605);
        textArea.setStyle("-fx-effect: innershadow(two-pass-box, gray, 5, 0.3, 1, 1);");
        paneUpdate2.getChildren().add(textArea);

        // set label on paneUpdate for paneUpdate1, paneUpdate2
        inputFields.setLayoutX(10.0);
        inputFields.setLayoutY(0.0);

        wolffAddressManager.setLayoutX(430.0);
        wolffAddressManager.setLayoutY(0.0);

        // paneUpdate for adding 2 panes
        paneUpdate.setStyle("-fx-background-color: lightgrey;");

        // add panes, nodes to paneUpdate
        paneUpdate.getChildren().addAll(paneUpdate1, paneUpdate2, wolffAddressManager, inputFields);
        model.setPaneUpdate(paneUpdate);
        model.setSceneUpdate(new Scene(model.getPaneUpdate(), 800, 675));
        return model.getSceneUpdate();
    }

    public void setTextArea(String text) {
        textArea.setText(text);
    }

    public String getTextArea() {
        return textArea.getText();
    }                
}

Then there is my Model I use for both VIEWS

Model

public class AddressManagerMVCJavaFXModel {
    private String updateEmailAddress;
    private String companyName;
    private String firstName;
    private String lastName;
    private String emailAddress;
    private String reTypeEmailAddress;
    private String street;
    private String number;
    private String additive;
    private String zipCode;
    private String city;
    private TextField TextFieldUpdateEmailAddress;
    private TextField TextFieldCompanyName;
    private TextField TextFieldFirstName;
    private TextField TextFieldLastName;
    private TextField TextFieldEmailAddress;
    private TextField TextFieldReTypeEmailAddress;
    private TextField TextFieldStreetName;
    private TextField TextFieldHouseNumber;
    private TextField TextFieldAdditive;
    private TextField TextFieldZipCode;
    private TextField TextFieldCityName;
    private TextField TextFieldSearchEmailAddress;
    private TextField TextFieldRemoveEmailAddress;
    private ArrayList<String> searchResultArray = null;
    private ArrayList<String> updateResultArray = null;
    private Scene scene;
    private Scene sceneUpdate;
    private Pane rootPane;
    private Pane paneUpdate;

    // updateView Model
    public Scene getSceneUpdate() {
        return sceneUpdate;
    }

    public void setSceneUpdate(Scene sceneUpdate) {
        this.sceneUpdate = sceneUpdate;
    }

    public Pane getPaneUpdate() {
        return paneUpdate;
    }

    public void setPaneUpdate(Pane paneUpdate) {
        this.paneUpdate = paneUpdate;
    }

    // Main view Model
    public Pane getRootPane() {
        return rootPane;
    }

    public void setRootPane(Pane rootPane) {
        this.rootPane = rootPane;
    }

    public Scene getScene() {
        return scene;
    }

    public void setScene(Scene scene) {
        this.scene = scene;
    }

    public String getUpdateEmailAddress() {
        return updateEmailAddress;
    }

    public void setUpdateEmailAddress(String updateEmailAddress) {
        this.updateEmailAddress = updateEmailAddress;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    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 getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getReTypeEmailAddress() {
        return reTypeEmailAddress;
    }

    public void setReTypeEmailAddress(String reTypeEmailAddress) {
        this.reTypeEmailAddress = reTypeEmailAddress;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getAdditive() {
        return additive;
    }

    public void setAdditive(String additive) {
        this.additive = additive;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public TextField getTextFieldUpdateEmailAddress() {
        return TextFieldUpdateEmailAddress;
    }

    public void setTextFieldUpdateEmailAddress(TextField updateEmailAddress) {
        this.TextFieldUpdateEmailAddress = updateEmailAddress;
    }

    public TextField getTextFieldCompanyName() {
        return TextFieldCompanyName;
    }

    public void setTextFieldCompanyName(TextField companyName) {
        this.TextFieldCompanyName = companyName;
    }

    public TextField getTextFieldFirstName() {
        return TextFieldFirstName;
    }

    public void setTextFieldFirstName(TextField fName) {
        this.TextFieldFirstName = fName;
    }

    public TextField getTextFieldLastName() {
        return TextFieldLastName;
    }

    public void setTextFieldLastName(TextField lastName) {
        this.TextFieldLastName = lastName;
    }

    public TextField getTextFieldEmailAddress() {
        return TextFieldEmailAddress;
    }

    public void setTextFieldEmailAddress(TextField emailAddress) {
        this.TextFieldEmailAddress = emailAddress;
    }

    public TextField getTextFieldReTypeEmailAddress() {
        return TextFieldReTypeEmailAddress;
    }

    public void setTextFieldReTypeEmailAddress(TextField reTypeEmailAddress) {
        this.TextFieldReTypeEmailAddress = reTypeEmailAddress;
    }

    public TextField getTextFieldStreet() {
        return TextFieldStreetName;
    }

    public void setTextFieldStreet(TextField streetName) {
        this.TextFieldStreetName = streetName;
    }

    public TextField getTextFieldHouseNumber() {
        return TextFieldHouseNumber;
    }

    public void setTextFieldHouseNumber(TextField houseNumber) {
        this.TextFieldHouseNumber = houseNumber;
    }

    public TextField getTextfieldAdditive() {
        return TextFieldAdditive;
    }

    public void setTextFieldAdditive(TextField additive) {
        this.TextFieldAdditive = additive;
    }

    public TextField getTextFieldZipCode() {
        return TextFieldZipCode;
    }

    public void setTexFieldZipCode(TextField zipCode) {
        this.TextFieldZipCode = zipCode;
    }

    public TextField getTextFieldCity() {
        return TextFieldCityName;
    }

    public void setTextFieldCity(TextField cityName){
        this.TextFieldCityName = cityName;
    }

    public TextField getTextFieldSearchEmailAddress(){
        return TextFieldSearchEmailAddress;
    }

public void setTextFieldSearchEmailAddress(TextField searchEmailAddress) {
    this.TextFieldSearchEmailAddress = searchEmailAddress;
}

public TextField getTextFieldRemoveEmailAddress(){
    return TextFieldRemoveEmailAddress;
}

public void setTextFieldRemoveEmailAddress(TextField removeEAddress){
    this.TextFieldRemoveEmailAddress = removeEAddress;
}

public ArrayList<String> getSearchResultArray() {
    return searchResultArray;
}

public void setSearchResultArray(ArrayList searchResultArray) {
    this.searchResultArray = searchResultArray;
}

public ArrayList<String> getUpdateResultArray() {
    return updateResultArray;
}

public void setUpdateResultArray(ArrayList searchResultArray) {
    this.updateResultArray = searchResultArray;
}

}

Next is my controller class. But I'm not so sure if I have put the right things in the controller

Controller

public class AddressManagerMVCJavaFXController {
    private final AddressManagerMVCJavaFXModel model;
    private final WolffAddressManagerQueriesMVCSERVICES query;
    private final AddressManagerMVCJavaFXView view = new AddressManagerMVCJavaFXView();
    //private AddressManagerMVCUpdateJavaFXView updateView = new AddressManagerMVCUpdateJavaFXView();

    public AddressManagerMVCJavaFXController(AddressManagerMVCJavaFXModel model) {
        this.query = new WolffAddressManagerQueriesMVCSERVICES();
        this.model = model;
    }

    public void buildView1(Stage primaryStage) throws Exception {
        view.start(primaryStage);
    }

    public void buildView2() throws Exception {
        Stage stage = new Stage();
         stage.setTitle("WolffAddressManagerUpdateView");
         stage.setScene(new AddressManagerMVCUpdateJavaFXView().createScene());
         stage.show();

    }

    public void addCredentials(AddressManagerMVCJavaFXView view , AddressManagerMVCJavaFXModel model) throws Exception {
        if(model.getEmailAddress().equals(model.getReTypeEmailAddress())) {
            if(query.saveAddressToDataBase(view, model) == true) {
                showCredentials(view);
            }
            else {
                view.setTextArea(view.getTextArea() + "EMAIL ALREADY EXISTS\n\n");
            }
        } else {
            JOptionPane.showMessageDialog(null,  "Emailaddresses are not the same", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void showCredentials(AddressManagerMVCJavaFXView view) throws Exception {
        DefaultListModel<String> jList = new DefaultListModel();
        JList<String> list = new JList(jList);
        model.setSearchResultArray(searchCredentials(model));
        for(int i = 0; i < model.getSearchResultArray().size(); i++) {
            jList.addElement(model.getSearchResultArray().get(i));
        }
        list.setModel(jList);

        int size = list.getModel().getSize();
        StringBuilder credentials = new StringBuilder();

        for(int i = 0; i < size; i++) {
           credentials.append(list.getModel().getElementAt(i)).append("\n");
        }
        view.setTextArea(view.getTextArea() + credentials.toString()+ "CREDENTIALS ADDED\n\n");
    }

    public void removeCredentials() throws Exception {
        query.removeAddress(model.getEmailAddress());
    }

    public ArrayList<String> searchCredentials(AddressManagerMVCJavaFXModel model) throws Exception {
        return query.searchAddress(model.getEmailAddress());
    }

    public void showSearchResult(AddressManagerMVCJavaFXView view, AddressManagerMVCJavaFXModel model) {
        DefaultListModel<String> jList = new DefaultListModel();
        JList<String> list = new JList(jList);

        try {
            model.setSearchResultArray(searchCredentials(model));
        } catch (Exception e) {
            Logger.getLogger(MainWindowEventHandlerSearchMVCJavaFXView.class.getName()).log(Level.SEVERE, null, e);
        } 
        for(int i = 0; i < model.getSearchResultArray().size(); i++) {
            jList.addElement(model.getSearchResultArray().get(i));
        }
        list.setModel(jList);

        int size = list.getModel().getSize();
        StringBuilder credentials = new StringBuilder();
        for(int i = 0; i < size; i++) {
           credentials.append(list.getModel().getElementAt(i)).append("\n");
        }
        view.setTextArea(view.getTextArea() + credentials.toString());
    }

    private void showUpdateResult(AddressManagerMVCUpdateJavaFXView view, AddressManagerMVCJavaFXModel model) {
        DefaultListModel<String> jList = new DefaultListModel();
        JList<String> list = new JList(jList);

        try {
            model.getUpdateResultArray();
        } catch (Exception e) {
            Logger.getLogger(MainWindowEventHandlerSearchMVCJavaFXView.class.getName()).log(Level.SEVERE, null, e);
        } 
        for(int i = 0; i < model.getUpdateResultArray().size(); i++) {
            jList.addElement(model.getUpdateResultArray().get(i));
        }
        list.setModel(jList);

        int size = list.getModel().getSize();
        StringBuilder credentials = new StringBuilder();
        for(int i = 0; i < size; i++) {
           credentials.append(list.getModel().getElementAt(i)).append("\n");
        }
        view.setTextArea(credentials.toString());
    }

    public void updateCredentials(AddressManagerMVCUpdateJavaFXView view,AddressManagerMVCJavaFXModel model) throws Exception {
        if(query.updateAddress(view, model) == true) {
            showUpdateResult(view,model);
        }
    }
}

For each button I created a different Action Event Class. Which I think still belongs to the VIEW. I'll post one of them.

EventHandler

public class MainWindowEventHandlerAddMVCJavaFZView implements EventHandler {

    AddressManagerMVCJavaFXModel model = new AddressManagerMVCJavaFXModel();
    AddressManagerMVCJavaFXView view;
    AddressManagerMVCJavaFXController controller = new AddressManagerMVCJavaFXController(model);

    public MainWindowEventHandlerAddMVCJavaFZView(AddressManagerMVCJavaFXView view, TextField companyName, TextField firstName, TextField lastName, TextField emailAddress, TextField reTypeEmailAddress, TextField streetName, TextField houseNumber, TextField additive, TextField zipCode, TextField cityName) {
        this.view = view;
        this.model.setTextFieldCompanyName(companyName);
        this.model.setTextFieldFirstName(firstName);
        this.model.setTextFieldLastName(lastName);
        this.model.setTextFieldEmailAddress(emailAddress);
        this.model.setTextFieldReTypeEmailAddress(reTypeEmailAddress);
        this.model.setTextFieldStreet(streetName);
        this.model.setTextFieldHouseNumber(houseNumber);
        this.model.setTextFieldAdditive(additive);
        this.model.setTexFieldZipCode(zipCode);
        this.model.setTextFieldCity(cityName);
        this.model.setTextFieldSearchEmailAddress(companyName);
    }    


    @Override
    public void handle(Event event) {
        model.setCompanyName(model.getTextFieldCompanyName().getText());
        model.setFirstName(model.getTextFieldFirstName().getText());
        model.setLastName(model.getTextFieldLastName().getText());
        model.setEmailAddress(model.getTextFieldEmailAddress().getText());
        model.setReTypeEmailAddress(model.getTextFieldReTypeEmailAddress().getText());
        model.setStreet(model.getTextFieldStreet().getText());
        model.setNumber(model.getTextFieldHouseNumber().getText());
        model.setAdditive(model.getTextfieldAdditive().getText());
        model.setZipCode(model.getTextFieldZipCode().getText());
        model.setCity(model.getTextFieldCity().getText());
        try {
            controller.addCredentials(view, model);
        } catch (Exception ex) {
            Logger.getLogger(MainWindowEventHandlerAddMVCJavaFZView.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }
}

I also have a seperate class for the queries

Queries

public class AddressManagerQueriesMVCJavaFXServices {
    private Connection connect = null;
    private final Statement statement = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    public boolean updateAddress(AddressManagerMVCUpdateJavaFXView view, AddressManagerMVCJavaFXModel model) throws Exception {
        boolean updateAddress = true;
        ArrayList<String> updateAddressArray = new ArrayList();
        int count = 0;
        try {
  //        This will load the MySQL driver, each DB has its own driver
            Class.forName("com.mysql.jdbc.Driver");
  //        Setup the connection with the DB
            connect = DriverManager.getConnection("jdbc:mysql://localhost/dev?" + "user=root&password=somePassword");
            preparedStatement = connect.prepareStatement("SELECT * FROM dev.addresses WHERE emailaddress= '" + model.getUpdateEmailAddress() + "'");
            resultSet = preparedStatement.executeQuery();


                if(resultSet.next()) {
                    Object[] options = { "YES", "NO" };
                    int reply = JOptionPane.showOptionDialog(null, "Are you sure you want to update the crendentials of: " + resultSet.getString("firstname") + " " + resultSet.getString("lastname"), "WARNING.", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
                    if(reply == JOptionPane.YES_OPTION) {
                        preparedStatement = connect.prepareStatement("UPDATE dev.addresses SET companyname = ?, firstname = ?, lastname = ?, emailaddress = ?, retype_emailaddress = ?, street = ?, housenumber = ?, additive = ?, zipcode = ?, city = ? WHERE id = " + resultSet.getString("id"));
                        if(!model.getCompanyName().equals("")) {
                            count++;
                            updateAddressArray.add("Updated Company Name: " + model.getCompanyName());
                            preparedStatement.setString(1, model.getCompanyName());
                        } else {
                            preparedStatement.setString(1, resultSet.getString("companyname"));
                        }
                        if(!model.getFirstName().equals("")) {
                            count++;
                            updateAddressArray.add("Updated First Name: " + model.getFirstName());
                            preparedStatement.setString(2, model.getFirstName());
                        } else {
                            preparedStatement.setString(2, resultSet.getString("firstname"));
                        }
                        if(!model.getLastName().equals("")) {
                            count++;
                            updateAddressArray.add("Updated Last Name: " + model.getLastName());
                            preparedStatement.setString(3, model.getLastName());
                        } else {
                            preparedStatement.setString(3, resultSet.getString("lastname"));
                        }
                        if(!model.getEmailAddress().equals("")) {
                            count++;
                            updateAddressArray.add("Updated Email Address: " + model.getEmailAddress());
                            preparedStatement.setString(4, model.getEmailAddress());
                        } else {
                            preparedStatement.setString(4, resultSet.getString("emailaddress"));
                        }
                        if(!model.getReTypeEmailAddress().equals("")) {
                            count++;
                            preparedStatement.setString(5, model.getEmailAddress());
                        } else {
                            preparedStatement.setString(5, resultSet.getString("retype_emailaddress"));
                        }
                        if(!model.getStreet().equals("")) {
                            count++;
                            updateAddressArray.add("Updated Street: " + model.getStreet());
                            preparedStatement.setString(6, model.getStreet());
                        } else {
                            preparedStatement.setString(6, resultSet.getString("street"));
                        }
                        if(!model.getNumber().equals("")) {
                            count++;
                            updateAddressArray.add("Updated Number: " + model.getNumber());
                            preparedStatement.setString(7, model.getNumber());
                        } else {
                            preparedStatement.setString(7, resultSet.getString("housenumber"));
                        }
                        if(!model.getAdditive().equals("")) {
                            count++;
                            updateAddressArray.add("Updated Additive: " + model.getAdditive());
                            preparedStatement.setString(8, model.getAdditive());
                        } else {
                            preparedStatement.setString(8, resultSet.getString("additive"));
                        }
                        if(!model.getZipCode().equals("")) {
                            count++;
                            updateAddressArray.add("Updated Zipcode: " + model.getZipCode());
                            preparedStatement.setString(9, model.getZipCode());
                        } else {
                            preparedStatement.setString(9, resultSet.getString("zipcode"));
                        }
                        if(!model.getCity().equals("")) {
                            count++;
                            updateAddressArray.add("Updated City: " + model.getCity() + "\n");
                            preparedStatement.setString(10, model.getCity());
                        } else {
                            preparedStatement.setString(10, resultSet.getString("city"));
                        }
                        updateAddressArray.add("\n\n" + count + " UPDATED FIELDS\n\n");
                        model.setUpdateResultArray(updateAddressArray);
                        preparedStatement.executeUpdate();
                        JOptionPane.showMessageDialog(null,  "Credentails of:  " + resultSet.getString("firstname") + " " + resultSet.getString("lastname") + " is updated", "InfoBox: " + "Updated", JOptionPane.INFORMATION_MESSAGE);
                    }
            } else {
                updateAddress = false;
                JOptionPane.showMessageDialog(null,  "Emailaddress:  " + model.getUpdateEmailAddress()+ " doesn't exist", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE);

              }   
        } catch (ClassNotFoundException | SQLException | HeadlessException e) {
          throw e;
        } finally {
          close();
        }
        return updateAddress;
    }

    public boolean saveAddressToDataBase(AddressManagerMVCJavaFXView view, AddressManagerMVCJavaFXModel model) throws Exception {
        boolean test = false;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/dev?" + "user=root&password=somePassword");
            preparedStatement = connect.prepareStatement("SELECT emailaddress FROM dev.addresses WHERE emailaddress= '" + model.getEmailAddress() + "'");
            resultSet = preparedStatement.executeQuery();

            if(resultSet.next()) {
                JOptionPane.showMessageDialog(null,  "Emailaddress:  " + model.getEmailAddress()+ " already exist", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE);
            } else {
                preparedStatement = connect.prepareStatement("INSERT INTO  dev.addresses (companyname, firstname, lastname, emailaddress, retype_emailaddress, street, housenumber, additive, zipcode, city) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                preparedStatement.setString(1, model.getCompanyName());
                preparedStatement.setString(2, model.getFirstName());
                preparedStatement.setString(3, model.getLastName());
                preparedStatement.setString(4, model.getEmailAddress());
                preparedStatement.setString(5, model.getReTypeEmailAddress());
                preparedStatement.setString(6, model.getStreet());
                preparedStatement.setString(7, model.getNumber());
                preparedStatement.setString(8, model.getAdditive());
                preparedStatement.setString(9, model.getZipCode());
                preparedStatement.setString(10, model.getCity());
                preparedStatement.executeUpdate();
                test = true;
            }
        } catch (ClassNotFoundException | SQLException | HeadlessException e) {
          throw e;
        } finally {
          close();
        }
        return test;
    }

    public ArrayList<String> searchAddress(String emailAddress) throws Exception {
        ArrayList<String> queryResultSet = new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/dev?" + "user=root&password=somePassword");
            preparedStatement = connect.prepareStatement("SELECT * FROM dev.addresses WHERE emailaddress= '" + emailAddress + "'");
            resultSet = preparedStatement.executeQuery();
                if(resultSet.next()) {
                    queryResultSet.add("CREDENTIALS OF: " + resultSet.getString("firstname").toUpperCase() + " " + resultSet.getString("lastname").toUpperCase() + "\n");
                    queryResultSet.add("Company Name: " + resultSet.getString("companyname"));
                    queryResultSet.add("First Name: " + resultSet.getString("firstname"));
                    queryResultSet.add("Last Name: " + resultSet.getString("lastname"));
                    queryResultSet.add("Email: " + resultSet.getString("emailaddress"));
                    queryResultSet.add("Street Name:  " + resultSet.getString("street"));
                    queryResultSet.add("House Number: " + resultSet.getString("housenumber"));
                    queryResultSet.add("Additive: " + resultSet.getString("additive"));
                    queryResultSet.add("Zipcode: " + resultSet.getString("zipcode"));
                    queryResultSet.add("City: " + resultSet.getString("city") + "\n");
                } else {
                JOptionPane.showMessageDialog(null, emailAddress + " doesn't exist", "INVALID INPUT.", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (ClassNotFoundException | SQLException | HeadlessException e) {
          throw e;
        } finally {
          close();
        }
        return queryResultSet;
    }

    public void removeAddress(String emailAddress) throws Exception {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/dev?" + "user=root&password=somePassword");
            preparedStatement = connect.prepareStatement("SELECT * FROM dev.addresses WHERE emailaddress= '" + emailAddress + "'");
            resultSet = preparedStatement.executeQuery();

            if(resultSet.next()) {
                Object[] options = { "YES", "NO" };
                int reply = JOptionPane.showOptionDialog(null, "Are you sure you want to delete the crendentials of: " + resultSet.getString("firstname") + " " + resultSet.getString("lastname"), "WARNING.", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
                if(reply == JOptionPane.YES_OPTION) {
                    preparedStatement = connect.prepareStatement("DELETE FROM dev.addresses WHERE emailaddress = '" + emailAddress + "'");
                    preparedStatement.executeUpdate();
                }
            } else {
                JOptionPane.showMessageDialog(null,"Emailaddress: " + "\"" + emailAddress + "\"" + " Doesn't exist", "INVALID INPUT.", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (ClassNotFoundException | SQLException | HeadlessException e) {
          throw e;
        } finally {
          close();
        }
    }

  // Close the resultSet
    private void close() {
        try {
          if (resultSet != null) {
            resultSet.close();
          }

          if (statement != null) {
            statement.close();
          }

          if (connect != null) {
            connect.close();
          }
        } catch (Exception e) {

        }
    }
}

I created this application mainly on what I read on the Internet but mostly on Stack Overflow. I just would like to know from other programmers if I'm on the right track.

\$\endgroup\$
1
\$\begingroup\$

Model

The model should contain the business logic independent of how various elements are displayed on screen. Your model references UI classes like TextField but it shouldn't care from where it gets it's inputs. In your case a model would expose methods like AddressEntity getAddressOfUserByName(string name) which return objects that contain the various fields in your database.

One way to know you're on the right track is to be able to use your model classes in a different project that doesn't use JavaFX without needing to change anything.

Controller and View

Your controller knows to much about the view. It doesn't have to care that the view has that text field or that list box. That's the view's responsibility. The controller asks the view to displayAddressResults(...) and the view makes sure to update all the required elements. That way you can drastically change the view without needing to also change the controller.

\$\endgroup\$
  • \$\begingroup\$ Okay. I thought the Model should hold all the datafields that exist through the whole application. So you could change their values through setters and that you could use the same datafields for different actions. So if I understand it right the model also performs CRUD on the database? How does the view change if the contoller knows less of the view? \$\endgroup\$ – coos wolff May 28 '16 at 18:39
  • 1
    \$\begingroup\$ The model provides methods for CRUD operations. The controller queries the model for something, the model returns an answer (an entity class that has fields for firstName, ...) which the controller then forwards to the view. The view (AddressManagerFXView) implements an interface AddressManagerView that provides the controller with methods such as void displayAddressResults(...). Only the view class has access to specific JavaFX classes. You can then later implement a, say, AddressManagerSwingView and realise you only change the view code, the rest of the logic remaining the same. \$\endgroup\$ – D. Jurcau May 28 '16 at 18:52
  • \$\begingroup\$ Okay. I think I understand it now. \$\endgroup\$ – coos wolff May 28 '16 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.