Display Name
Display Name

Reputation: 405

Handling different users in JavaFX

I am learning Java and JavaFX. I have an application that has some ComboBox components that are only visible if logged in as an admin My problem is that the application populates the Combobox with system variables when it starts, If a user logs in then i get a null pointer and the application doesnt start because the ComboBox doesnt exist.. When an admin logs in the application starts correctly. Here is how i am trying to get it to work.

private void loginpressed(ActionEvent event) throws IOException

{

    if (BCrypt.checkpw(userId.getText() + passwordfield.getText(), passwordhashuser))
        {
    Parent root = FXMLLoader.load(getClass().getResource("LaserControllerUserUI.FXML"));
    Scene home_page_scene = new Scene(root);
    Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    app_stage.setScene(home_page_scene);
    app_stage.show();

        }
    else if (BCrypt.checkpw(userId.getText() + passwordfield.getText(), passwordhashadmin))
    {
        Parent root = FXMLLoader.load(getClass().getResource("LaserControllerUI.FXML"));
        Scene home_page_scene = new Scene(root);
        Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        app_stage.setScene(home_page_scene);
        app_stage.show();
    } 
    else{
        errorMessage.setText("Login Incorrect!");
        System.out.print("false");}
}

So i have two seperate FXML depending on who logs in. How do i handle this?

Upvotes: 0

Views: 1810

Answers (2)

SedJ601
SedJ601

Reputation: 13859

I would use one FXML Document. If they have pretty much the same components, I would set the locations and visibility of the components after a user logs in. What is set to visible should depend on the users account type.

Upvotes: 1

Derrops
Derrops

Reputation: 8127

In general, you should avoid making your application in a way in which it will need administrative privileges, if it does need these privileges you will need to give this feedback to the user, or enforce this condition, you can google how to do this such as in this link for Microsoft: https://technet.microsoft.com/en-us/library/ff431742.aspx. Note that you can have access to the current user through the System properties in Java, and you can also save properties based on a user as well, if that helps.

That casting your doing completely unnecessary. I would look up some GUI tutorials on MVC for Java if I was you. Once you learn this pattern you will have a light bulb moment and everything will just make sense. There are alterations of this pattern you should learn the main ones.

Not following any GUI patterns will cause you immense pain and complications. I would estimate over 100,000 questions on SO would not exist if people knew this pattern. This includes your question. GUI and functionality should be completely separated. Your question would simply be: how do I get a list of all Users, which would be a duplicate and not need to be asked.

Upvotes: 1

Related Questions