0

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;

Application Class

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);
    }
}

Controller Class

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;
}

In Addition, AttackSimulator class defined as 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.

quartaela
  • 2,579
  • 16
  • 63
  • 99
  • What kind of safety are you bother about? Can you lighten this further. – Uluk Biy Sep 09 '13 at 22:56
  • 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. – quartaela Sep 09 '13 at 23:01
  • 2
    "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. – Uluk Biy Sep 09 '13 at 23:39

1 Answers1

1

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.
  • Don't use @FXML to inject a static value.

See related questions and answers for further inspiration.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • 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 Sep 10 '13 at 18:33