Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Multiple scene and passing variables with FXML files

2909064Mar 21 2015 — edited Mar 23 2015

Ok lets say I have a desire to convert and entered value to Centigrade or Fahrenheit and that I have two Radio Buttons to designate the type of conversion with the Answer posted to a TextField on Scene Two from a button click event on Scene One ok Now I would like to convert the value in the TextField on Scene Two to Kelvin and reflect the value on Scene Three in a TextField by clicking a button on Scene Two

lets say  I have 4 FXML files sceneONE sceneTWO and scenMain and I created 4 controllers ControllerONE and MasterController

so far I can navigate to the different scens with just the MasterController code below

BUT because I have two FXML files I can not capture the value in the first FXML file and pass it to the second scene

This project is my attempt to adapt the FXML style code from this web site

Switching to Different Screens in JavaFX and FXML | JavaFXTutorials

which uses this code to switch scenes

    //get reference to the button's stage        

       stage=(Stage) btnS1.getScene().getWindow();

       //load up OTHER FXML document

       root = FXMLLoader.load(getClass().getResource("ATTwo.fxml"));

but will not permit variable transfer due to scope of variables

which reside in two different FXML files

public class MasterController implements Initializable{

@FXML public Button btnS1;

@FXML public Button btnS2;

@FXML public Label lblS1;

@FXML public Label lblS2;

@FXML public TextField txfS1;

@FXML public TextField txfS2;

@FXML public AnchorPane root;

Stage stage;

@FXML

private void hbtnClick(ActionEvent event)  throws IOException {

  

    if(event.getSource()==btnS1){

    //get reference to the button's stage        

       stage=(Stage) btnS1.getScene().getWindow();

       //load up OTHER FXML document

       root = FXMLLoader.load(getClass().getResource("ATTwo.fxml"));

         

       System.out.println("We are above " + txfS1.getText());

    

       //FA = (Float) (32 * Float.parseFloat(txfS1.getText()));

       //txfS2.setText(String.valueOf(FA));    

    }else {

    stage=(Stage) btnS2.getScene().getWindow();

    root = FXMLLoader.load(getClass().getResource("ATOne.fxml"));

    System.out.println("We are down " + txfS2.getText());   

      //FA = txfS1.getText();

      //txfS2.setText(FA);

      //FC = (Float) (Float.parseFloat(txfS2.getText())/32 + 1);

  //if (FA == 160) {

  //FC = (float) 1.0;

  //}

  //txfS1.setText(String.valueOf(FC));

    }

      

    //create a new scene with root and set the stage

    Scene scene = new Scene(root);    

    stage.setScene(scene);

    stage.show();

  }

  @Override

  public void initialize(URL location, ResourceBundle resources) {

  }

  }

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2015
Added on Mar 21 2015
4 comments
6,489 views