Problem passing parameters to a Java controller

Asked

Viewed 31 times

1

I am having a problem trying to pass parameters to any controller, using as example two of my controllers:

The controller1 has the following function to navigate to controller2:

public void navegarParaTelaProfessor(Professor professor){
        try{
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/View/telaProfessor/professorTela/T1 Professor.fxml"));
            Parent root = fxmlLoader.load();

            (((ControllerT1) fxmlLoader.getController())).setStage(stage, professor, new FachadaProfessor());

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Professor");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

And in the controller2 we have the function that stores the parameters passed:

public void setStage(Stage stage, Professor professor, FachadaProfessor fachadaProfessor) throws IOException, ClassNotFoundException {
        this.stage = stage;
        fachadaProfessor.recuperarTurmasProfessor(professor);
        this.professor = professor;
        this.fachadaProfessor = fachadaProfessor;
        this.turmas = FXCollections.observableArrayList(this.professor.getTurmasArrayList());
    }

Soon after passing the parameters the Scene change and the layout update attempt is made, however, all objects are null when entering the controller:

inserir a descrição da imagem aqui

However, during the function call of the parameters all objects are saved correctly: inserir a descrição da imagem aqui

Any idea what you might be doing wrong? Complete code of the controller 2:

package Controller.ControllersTelaProfessor;

import Classes.pessoas.Professor;
import Classes.turmas.Turma;
import Controller.ControllerLogin;
import Model.fachada.FachadaProfessor;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class ControllerT1 implements Initializable {
    private String mensagem;
    private FachadaProfessor fachadaProfessor;
    private Professor professor;
    private Turma turmaSelecionada;
    private ObservableList<Turma> turmas;

    private Stage stage;


    @FXML
    private ListView<Turma> listaTurmas;

    @FXML
    private void setTurmaSelecionada(){
        Turma turma = this.listaTurmas.getSelectionModel().getSelectedItem();

        this.turmaSelecionada = turma;
    }

    @FXML
    public void voltar() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/View/telaLogin/TelaLogin.fxml"));
        Parent root = fxmlLoader.load();

        ((ControllerLogin) fxmlLoader.getController()).setStage(this.stage);

        Scene scene = new Scene(root);
        this.stage.setScene(scene);
        this.stage.setTitle("Login");
    }


    @FXML
    public void continuar(){
        try{
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/View/telaProfessor/professorTela/T2 Professor.fxml"));
            Parent root = fxmlLoader.load();

            ((ControllerT2) fxmlLoader.getController()).setStage(this.stage);
            ((ControllerT2) fxmlLoader.getController()).setParametros(this.fachadaProfessor, this.turmaSelecionada, this.professor);

            Scene scene = new Scene(root);
            this.stage.setScene(scene);
            this.stage.setTitle(turmaSelecionada.getApelido());

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void iniciarLayout(){
        listaTurmas.setItems(turmas);
    }

    public void setStage(Stage stage, Professor professor, FachadaProfessor fachadaProfessor) throws IOException, ClassNotFoundException {
        this.stage = stage;
        fachadaProfessor.recuperarTurmasProfessor(professor);
        this.professor = professor;
        this.fachadaProfessor = fachadaProfessor;
        this.turmas = FXCollections.observableArrayList(this.professor.getTurmasArrayList());
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        iniciarLayout();
    }
}
No answers

Browser other questions tagged

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