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.

4

1 回答 1

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.

于 2013-09-10T05:03:26.080 回答