0

I have a project login system

Main Scene: CMainWallet.java

@FXML private Label status;
@FXML private Label version;
@FXML public TextField user;
@FXML public TextField pass;
@FXML private Button login;
@FXML private Button exit;

@FXML
public void loginWallet(ActionEvent event) {

    String u = user.getText();
    String p = pass.getText();

    LoginModel loginModel = new LoginModel();

    if (loginModel.validationLogin(u, p)) {

        Stage stage = new Stage();

        try {
            Parent root = FXMLLoader.load(getClass().getResource("dashboard/Dashboard.fxml"));

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Wallet Dashboard");
            stage.show();
            login.getScene().getWindow().hide();

        } catch(Exception e) {

            e.printStackTrace();
        }
    } else {

        status.setText("Maaf Akun anda salah, Coba ulang lagi");
    }
}

Dashboard.java

@FXML Label user;

@Override
public void initialize(URL url, ResourceBundle rb) {

    CMainWallet main = new CMainWallet();

    user.setText("Hello "+main.user.getText());
}

And i want to get to take string textfied, in the CMainWallet.java Where is my mistake on code, is there any solution on my code

Thanks :D

rg16
  • 42
  • 1
  • 4

1 Answers1

0

try this

class Dashboard implements Initializable {
    @FXML Label user;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
//        CMainWallet main = new CMainWallet();

//        user.setText("Hello "+main.user.getText());
    }

    public void setUsername(String username) {
        user.setText(username);
    }
}

and main scene

@FXML
public void loginWallet(ActionEvent event) {

    String u = user.getText();
    String p = pass.getText();

    LoginModel loginModel = new LoginModel();

    if (loginModel.validationLogin(u, p)) {

        Stage stage = new Stage();

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("dashboard/Dashboard.fxml"));
            Parent root = loader.load();

            Dashboard dashboard = loader.getController();
            dashboard.setUsername(u);

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Wallet Dashboard");
            stage.show();
            login.getScene().getWindow().hide();

        } catch(Exception e) {

            e.printStackTrace();
        }
    } else {

        status.setText("Maaf Akun anda salah, Coba ulang lagi");
    }
}

I assume the rest of your logic is OK

mr mcwolf
  • 2,574
  • 2
  • 14
  • 27