-1

I am creating chat application using JavaFX and now I am working on chat window. And I am wondering if is it possible to create multiple instances of window like this using this method:

FXMLLoader.load(getClass().getResource("chatWindow.fxml"));

and setting all properties like name - Harry Potter, image etc. via getting controller and using setters. Does every window would have it's own controller? Or it's only one controller per all windows? Or I have to "translate" fxml code to Java code, and creating those scenes using Java? Like that:

VBox vbox = new VBox();
ofca1234
  • 53
  • 7
  • 3
    Yes, Yes, no and no. You can load a fxml as often as you want. For modifying the scenes take a look here: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml . You may want to [create custom components](https://docs.oracle.com/javase/9/docs/api/javafx/fxml/doc-files/introduction_to_fxml.html#custom_components) to use them from java code more easily however. – fabian Apr 09 '18 at 19:34
  • Okay, so what is better approach? Load everything from .fxml or create all from java? In this scenario. – ofca1234 Apr 09 '18 at 19:46
  • 2
    @ofca1234 Define "better"... Each has pros and cons – James_D Apr 09 '18 at 20:53
  • what happened when you tried all your alternatives? hey .. you are (or want or are forced to) be a developer - part of the job is to just try it out, see what happens and _after_ doing all these ask what/if you dont understand the outcome .. – kleopatra Apr 09 '18 at 22:45
  • @James_D More professional? Recommended way? Something like that. – ofca1234 Apr 10 '18 at 04:02
  • That's surely a matter of opinion... – James_D Apr 10 '18 at 09:36

1 Answers1

0

Simply: 1 FXML + 1 Controller

I don't know if I understood it correctly but maybe you want open new FXML after click or any other event. Then you can open another FXML which contains another components.

    try{
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("yourFXML.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setTitle("FXML");
        stage.setScene(new Scene(root1));  
        stage.show();
      }
Michal Moravik
  • 1,213
  • 1
  • 12
  • 23
  • 1
    You probably need to be more precise here. There should be one controller *class* for every FXML file. You get a new controller whenever you load an FXML file, whether or not it is the same FXML (and controller class) or not. – James_D Apr 09 '18 at 21:32