0

I am using the code below to pass the information, but i would like to know other ways. In the event handler method handleSubmitButtonAction of FXMLDocumentController, i create another window loading MainFXML file. After that, i get hold of its controller and send my information to Main Window. Is there a better way to do that?

public class FXMLDocumentController implements Initializable {

    @FXML
    private TextField user;

    @FXML
    public void handleSubmitButtonAction(ActionEvent event) throws IOException {
        Alert dialogo = new Alert(Alert.AlertType.INFORMATION, "User: " + user.getText() + " logged in.");
        dialogo.showAndWait();
        Stage stage2 = (Stage) user.getScene().getWindow();
        stage2.close();

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MainFXML.fxml"));        
        Parent root = (Parent) fxmlLoader.load();
        MainFXMLController controller = fxmlLoader.<MainFXMLController>getController();        

        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();

        controller.setUser(user.getText());        
    }
Rafael Coelho
  • 166
  • 3
  • 10
  • Using the Application as singleton instance "controller" in the MVC sense? Not pretty, but decouples between FX controller classes. – Joop Eggen Oct 14 '17 at 23:52
  • I would like to know which would be the best way. Every javafx application is MVC in its nature due to fxml file, right? – Rafael Coelho Oct 15 '17 at 00:15
  • Best way is just an opinion. Everybody can have different opinions. So you could get many different answers based on the different opinions and you would be no closer to determining the best way. Anyway, IMO, if it is large application, use a dependency injection mechanism, like [Gluon Ignite](http://gluonhq.com/labs/ignite/), make the injected objects JavaFX beans with observable properties and have your UI items bound or listen for changes on the properties. But, on the other hand, the solution you have in your question might be best for you, you decide. – jewelsea Oct 15 '17 at 09:28
  • Perhaps a duplicate of: [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). – jewelsea Oct 15 '17 at 09:30

1 Answers1

1

Trying to give a short answer.

What I do is that I create an "application model" of the controller classes. The root of the application model is of course the controller of the Application class. The application model does not leak the GUI elements, but tells the main program about being closable, having changed etc.

public abstract class Part {
       public final ObservableMap<String, ActionHandler> getActionHandlers() {...}    
       public final ObservableBooleanValue closableProperty() {...}
       public final ReadOnlyBooleanProperty disabledProperty() {...}
       ....
}

public abstract class ViewPart extends Part {
       public final StringProperty titleProperty() { ... }
       public final ReadOnlyObjectProperty<Image> iconProperty() { ... }
       ....
}

public abstract class Editor extends Part {
        public final ObservableBooleanValue dirtyProperty() { .... }
}

Like in Eclipse these parts can have their own window, but they not necessarily have, they can also be embedded in another window. This modeling is based loosely on Eclipse's structure.

M. le Rutte
  • 3,525
  • 3
  • 18
  • 31