Pass Data to other Javafx screens

Asked

Viewed 123 times

0

People who work with Javafx, I need a lot of help, I’m trying to pass data from one screen to another but I can’t.

In my project I log in with a user searching in the database. Each user has one id. I need, by the time he logs in, I can send this id to the main screen to be able to touch the other things with this id.

I have Login.Controller, login.fxml. login.tela, and the main screen the same thing, following the MVC standard.

In the code below I log in, at that time I wanted to send the id user to my main screen.

public void logar() {

        DAO<Usuarios> dao = new DAO<Usuarios>(Usuarios.class);
        List<Usuarios> users = dao.obterTodos();

        for (int i = 0; i < users.size(); i++) {

            if (txEmail.getText().equals(users.get(i).getEmail()) && txSenha.getText().equals(users.get(i).getSenha())) {
                idUsu = users.get(i).getId();//consigo guardar o id nessa varivel que criei
                Principal p = new Principal();
            
                i = users.size();
                fechar();
                try {
                    p.start(new Stage());
                    
                    Alert alert = new Alert(AlertType.CONFIRMATION);alert.setTitle(idUsu.toString());
                    alert.show();//fiz um teste de alerte pra ver se realmente pegava o id na variavel que criei
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
}

1 answer

0

I’d give you two choices

1st Make these attributes static so you can use it in which part of the code.

2nd You must not create a public method in the controller class that receives this data from the Start Screen (or any screen you want).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.