¿Es esta una implementación segura sobre la carga y el uso de dos archivos FXML en JavaFX 2.x?

I'm implementing an application which includes more than one FXML files.(Well there are just two different FXML files at the moment), and I want to ask that if the implementation is safe or do i need to do additional staff for improving it. Firstly, I have a main scene and its controller class like;

Clase de aplicación

public class JavaFXApplication1 extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {

        Parent rootPane = FXMLLoader.load(getClass().getResource("MainScene.fxml"));

        // Main scene
        Scene scene = new Scene(rootPane);        

        primaryStage.setScene(scene);
        primaryStage.show();

        MyControllerClass.startGame();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Clase de controlador

public class MyControllerClass implements Initializable {

    // Second Stage will be called later.
    public static Stage SecondStage;

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {

       //TODO

       SecondStage = new Stage();
       // Init Modality to use showAndWait() method
       SecondStage .initModality(Modality.APPLICATION_MODAL);

       try {
        Parent childPane= FXMLLoader.load(getClass().getResource("ChildScene.fxml"));
        SecondStage.setScene(new Scene(childPane));
        } catch (IOException ex) {
        Logger.getLogger(MyControllerClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void startGame() {

       // Start the game
       ...
       AttackSimulator.simulateAttack(0,0);
    }
..
}

So, I guess everything looks fine so far, but after some steps of application i have to use second FXML file without disposing the main scene.(Actually at the front of it). Now consider i have a class with some static methods, and I will show the second FXML file in front of the main scene with some updated components on it. Like;

AttackSimulator Class

public class AttackSimulator {

  public static void simulateAttack(int attackFrom, int attackTo){

        label1.setText("foobar1");
        label2.setText("foobar2");
        label3.setText("foobar3");
        label4.setText("foobar4");

        MyControllerClass.SecondStage.showAndWait();
   }

  @FXML
  private void ButtonActionHandler(ActionEvent event){

    // Do some calculations
    ...
    MyControllerClass.SecondStage.hide();
  }

  @FXML private static Label label1;
  @FXML private static Label label2;
  @FXML private static Label label3;
  @FXML private static Label label4;
}

Además, AttackSimulator clase definida como ChildScene.fxml 's controller class because it has action handler methods and imported components in it.

So, I know it seems a long question :),however, I want to know is this implementation looks like a safe one, because I have concerns about loading another fxml file in Controller Class of the main scene. I will appreciate for every response. Well thanks anyway.

preguntado el 09 de septiembre de 13 a las 23:09

What kind of safety are you bother about? Can you lighten this further. -

Actually, I intended to ask "is this a normal way" of implementation about ...? Because i read some blogs which mention that I have to use one controller per fxml. -

"One controller per fxml" is a proper, painless and expected way for normal, ordinary use cases. And you are already doing that. But your entire code design a bit "smells". Your app modules are tightly coupled. Instead of direct accessing to class instances through statics, use parameter passing via method or constructors. Well, let someone else give a comprehensive answer for you. -

1 Respuestas

Uluk's comment is pretty much the answer.

  • You should have one controller class per fxml (and you do).
  • You can load an fxml multiple times and each load will create a new unique instance (object) of your controller class.
  • Don't rely on static references so much.
  • No use @FXML to inject a static value.

See related questions and answers for further inspiration.

contestado el 23 de mayo de 17 a las 12:05

First of all, thanks for your reply :) I'm taking your time with my questions. The links are very obvious, and I decided that sending parameters to Controller Class will be better. - Quartaela

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.