0

Hello I need to pass The User and Pass Values to another Controller: like this AdminController ->UserController.

In my code I have This in AdminController:

private TextField fusuario;
    @FXML
   private PasswordField fcontrasena;
   String a;
   String b;



  public void captura() {
      a=fusuario.getText().toString();
      System.out.println("el usuario es x:"+a);
      b=fcontrasena.getText().toString();
      System.out.println("la contraseña es x:"+b);
  }

  public String setFusuario(String a) {
      this.fusuario.setText(a);
      return a;

     }
     public String setFcontrasena(String b) {
      this.fcontrasena.setText(b);
      return b;

     }

This is in my UserController:

                       Stage administrador=new Stage();
                        FXMLLoader carga = new FXMLLoader(getClass().getResource("Admin.fxml"));
                        Parent StackPane =(Parent) carga.load();
                        AdminScreenController control = carga.<AdminScreenController>getController();
                        control.deshabilitarespuesta();
                        Scene scene = new Scene(StackPane);
                        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                        administrador.setScene(scene);
                        administrador.setTitle("AdminScreen");
                        Stage userstage=(Stage)comentarios.getScene().getWindow();
                        userstage.hide();
                        administrador.show();

How i Pass this Values, some help or orientation?

  • 6
    Possible duplicate of [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – Slaw Oct 09 '19 at 00:11
  • 1
    Please take a [tour] and how to make a [mcve] – fuggerjaki61 Oct 09 '19 at 18:03
  • There is `deshabilitarespuesta` method in your first code snippet. This could be the probem unless you show only irrelevant parts (or parts you don't explain the relevance for) in the snippet. Also for some reason in your second code snippet it's called `AdminScreenController` instead of `AdminController`... – fabian Oct 09 '19 at 18:28

1 Answers1

0

This is easy you need to declare a static variable like this in the FIRST Controller

static String alertTYPE;

Then in our ERROR checking code we give alertTYPE a value

   if(txtAmount.getLength() == 0){
        alertTYPE = "1";
        customAlert();
        txtAmount.requestFocus();
        return;
    }

Now we call customAlert() method still in the same Controller class
Her is the customAlert method code

    public void customAlert() throws IOException{
    // This method displays the MODAL Alert alert.fxml and it is controlled by AlertController
    // =========================================================================================
    alertPane = FXMLLoader.load(getClass().getResource("alert.fxml"));// pane you are GOING TO

    //Scene Sscene = new Scene(merrorPane, 600, 400);
    // NO NEED TO RE-SIZE but this is code to change size 
    Scene Mscene = new Scene(alertPane);
    Mstage = new Stage();
    Mstage.initStyle(StageStyle.UNDECORATED);
    Mstage.setResizable(false);
    Mstage.initModality(Modality.APPLICATION_MODAL);
    Mstage.setScene(Mscene);

    Mstage.showAndWait();
}

Here in the AlertController we make use of the alertTYPE value

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

    if(alertTYPE.equals("1")){
        lblMessage.setText("Enter "+type+" Amount");
    }else if(alertTYPE.equals("2")){

You need to be sure the Controller you are going to imports the static variable alertTYPE like this

import static checkbook.CBManagerController.alertTYPE;

This was a long way around but we hope you get the idea
One word of caution once static variables are declared and given a value that value is retained until a new value is given to the variable or you set it to nothing like this for a String alertTYPE = ""; With great power comes great responsibility ha ha

Vector
  • 3,066
  • 5
  • 27
  • 54
  • Actually, no: static scope for passing stuff around is nearly always the worst option. Unrelated to the solution: please learn java naming conventions and stick to them. – kleopatra Oct 15 '19 at 10:34