1

Controller.java

public TextField l4M1ICT01;
public TextField l4M1ICT02;
public TextField l4M1CourseWork;
public TextField l4M1Average;
public int average_1;
public int credit_1;


public void l4M1AverageButtonClicked(ActionEvent event) throws IOException {

    int l4M1ICT01_1 = Integer.parseInt(l4M1ICT01.getText());
    int l4M1ICT02_1 = Integer.parseInt(l4M1ICT02.getText());
    int l4M1CourseWork_1 = Integer.parseInt(l4M1CourseWork.getText());
    average_1 = (l4M1ICT01_1 + l4M1ICT02_1 + l4M1CourseWork_1) / 3;
    l4M1Average.setText(average_1 + "");
    if(average_1 >= 40){
        credit_1 = 20;
    }else{
        credit_1 = 0;
    }
    System.out.println(credit_1);


}

public void l4M1NextButtonClicked(ActionEvent event) throws IOException {
    ((Node) (event.getSource())).getScene().getWindow().hide();
    //loading the next scene
    Parent parent = FXMLLoader.load(getClass().getResource("L4M2.fxml"));
    Stage stage = new Stage();
    Scene scene = new Scene(parent);
    stage.setScene(scene);
    stage.setTitle("Level 4 Module 2");
    //making the scene appear on the screen
    stage.show();
}

public TextField l4M2ICT01;
public TextField l4M2ICT02;
public TextField l4M2CourseWork;
public TextField l4M2Average;
public int average_2;



public void l4M2AverageButtonClicked(ActionEvent event) throws IOException {

    int l4M2ICT01_1 = Integer.parseInt(l4M2ICT01.getText());
    int l4M2ICT02_1 = Integer.parseInt(l4M2ICT02.getText());
    int l4M2CourseWork_1 = Integer.parseInt(l4M2CourseWork.getText());
    average_2 = (l4M2ICT01_1 + l4M2ICT02_1 + l4M2CourseWork_1) / 3;
    l4M2Average.setText(average_2 + "");
    System.out.println(credit_1);
}


public void l4M2NextButtonClicked(ActionEvent event) throws IOException {
    ((Node) (event.getSource())).getScene().getWindow().hide();
    //loading the next scene
    Parent parent = FXMLLoader.load(getClass().getResource("L4M3.fxml"));
    Stage stage = new Stage();
    Scene scene = new Scene(parent);
    stage.setScene(scene);
    stage.setTitle("Level 4 Module 3");
    //making the scene appear on the screen
    stage.show();
}

this is a portion of my javafx code i've been working on. on the first scene if i give 40+ as input for l4M1ICT01 it should save 20 as credit anything less than 40 should save or initialize the credit value to 0. when i print it inside the same 1st button click command it prints 20 for credit when i give a 40+ input then as soon as that method is over i tried to print the same value i got in the above method on my 2nd scene but now it only shows 0. how to pass that value through the scenes so i can use it as the end? thanks. I've used scene builder to code javafx.

Suwadith
  • 78
  • 1
  • 8
  • same thing happens with all the variables. they don't get stored at all. as soon as the scene changes all the variables are re initialized to 0 somehow. i need the value of each and every variable to finish my project so please help me. i'd like to have a fixed version of this code. thanks – Suwadith Mar 28 '16 at 11:16
  • You need to pass the values to the controllers for the other FXML files when you load them. – James_D Mar 28 '16 at 11:42
  • Well I'm using a single controller. there are no multiple controllers here. if you can check the above code there are 2 scenes. i can't pass the value through them. so can this be sorted out? – Suwadith Mar 28 '16 at 11:57
  • There are multiple controllers. The default behavior of the `FXMLLoader` is to create a controller instance when you load the fxml. So when you load the other FXML files in `l4M1NextButtonClicked()` and `l4M2NextButtonClicked()`, new controllers are created. You need to pass the data to them. – James_D Mar 28 '16 at 11:58
  • can you make a sample code for that? i need a code to transfer that credit between controllers. can you show me how it's done. that thread you referred is a bit confusing t me. i'm very new to the coding arena. i hope you do understand. – Suwadith Mar 28 '16 at 12:17
  • I can't make it any clearer than it is in the accepted answer to the linked question. It is a very bad idea (creates very confusing situations) to use the same controller class for controllers from different FXML files, so maybe you should start by creating different controller classes for each FXML. Then it should be easy to follow that answer. – James_D Mar 28 '16 at 12:21
  • i need a code to work around bro. this is my first time loading FXML files. so please can some one fix my code and post it here? – Suwadith Mar 28 '16 at 21:32
  • Again, all I can suggest is that you do it the way it's done in the linked answer. That is the way to fix your errors. – James_D Mar 28 '16 at 21:48

0 Answers0