0

I use one controller for two different purposes.

  1. Add Customers
  2. Add Employees

In order to make the controller function for both purposes I’ve used a static Boolean in the controller and set it to either true or false depending on what I want to do. It works like a charm, but using a static variable instead of sending data between the controllers when switching scenes isn’t very good (So I’ve been told anyway).

Edit: (more details) 17:40

Thanks for all replies, appreciate it! Here are some more details on the issue as requested:

The NewUserController class is the controller with the static boolean. AdminController class uses the controller to add employees and LogInController class use it to register customers. The object sw seen in the code is an instance of SwitchScene class.

Relevant code only:

NewUserController (not the complete code)


public static boolean isCustomer;

    public static void setCustomer(boolean customer) {
        isCustomer = customer;
    }

   if (isCustomer == true) {
                                            User customer = new Customer(stringFirstName,
                                                    stringLastName, IsMale, stringCountry, stringSSN, stringAddress, stringEmail, stringUserName, stringPassword);
                                            dbh.insertUser(customer, typeOfUser);
                                        } else if (isCustomer == false) {

                                            User customer = new Employee(stringFirstName,
                                                    stringLastName, IsMale, stringCountry, stringSSN, stringAddress, stringEmail, stringUserName, stringPassword);

                                            dbh.insertUser(customer, typeOfUser);
                                        }


                                        if (isCustomer == true) {

                                            sw.goToUnLogged(ae, "Login.fxml");
                                        }

                                        if (isCustomer == false) {
                                            sw.GoTo(ae, "Admin.fxml");

----------------------------------------------------------------------

AdminController (not complete code)

@FXML
    private void newEmployee(ActionEvent ae) {


        NewUserController.setCustomer(false);


        sw.GoTo(ae, "NewUser.fxml");


}

LoginController (not complete code)

@FXML
    private void newUser(ActionEvent ae) {
        NewUserController.setCustomer(true);

        sw.goToUnLogged(ae, "NewUser.fxml");
    }

SwitchScene(not complete code)

public void goToUnLogged(ActionEvent ae, String fxmlFile) {

        Node node = (Node) ae.getSource();
        Stage stage = (Stage) node.getScene().getWindow();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/" + "" + fxmlFile + ""));
        Parent root = null;
        try {
            root = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(root);
        stage.setScene(scene);

    }

I have checked out the other question you linked but I still haven’t got it to work. So, what’s the best way to avoid a static boolean in this case?

/ Peter

pwHKR
  • 1
  • 1
  • Are you saying you are using the same controller class for different controllers associated with different FXML files? I would start by simply not doing that and using different controller classes for each FXML. Then just pass data between the controllers as outlined in http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310. It's not really possible to give a detailed answer to this without understanding the relationship between the different views. – James_D May 04 '17 at 12:20
  • Thank you for the link! Just what I was looking for! – pwHKR May 04 '17 at 12:28

0 Answers0