简体   繁体   中英

Loading FXML from external controller on Java FX 2.2

I'm currently recoding a small application from Swing to JavaFX, since it seemed like the easiest way to deploy what I currently have for web.

I can't seem to do something very simple, and I'm getting lost on the documentation and other posts:

Anyway, I have my main controller that calls the associated FXML file:

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

Now, from Login.fxml, I have a button and I want that button to open another FXML file. I can get the button to be sucesfully load the event but I tried many things and I can't get it to work. I'm trying something like this:

private void handleButtonAction(ActionEvent event){

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("GeneradorBases.fxml"));

   // fxmlLoader.setRoot(this); 
  //  fxmlLoader.setController(this);

    try {
        fxmlLoader.load(); 

    }
    catch (IOException e){
       throw new RuntimeException(e); 
    }
}

I tried to follow an example I saw here on Stackoverflow. Basically the .setRoot and .setController crash the application. Even the .load() does that too.

Any advice on how I can make this work?

Given exception you listed you need to set all fields marked with @FXML and class packet.OtherControllerController public .

Update: you another problem is that you don't assign result of FXML load to anything.

public class FirstController implements Initializable {

  @FXML
  public void handleButtonAction(ActionEvent event) throws Exception{
    Node node = (Node) event.getSource();
    Stage stage = (Stage) node.getScene().getWindow();
    Scene scene = stage.getScene();

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second.fxml"));
    Parent root = (Parent) fxmlLoader.load();          

    scene.setRoot(root);
  }
}

See tutorial for extended example: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm

Your solution does nothing with the node loaded by fxmlLoader.load() . In order to show the loaded FXML file, you need to pass the returned value somehow to your main controller (or another part of the code that should manage the stage or scene) and set it as the new root (or eg child of a pane in which it should be shown).

If presented solutions don't will be helpful, you can try following code (for me it is work):

final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/page.fxml"));
      Parent root = (Parent) fxmlLoader.load();

      Scene nscene = new Scene(root);
      Stage tStatge = new Stage();
      tStatge.setScene(nscene);
      tStatge.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM