Mykhailo Pavliuk
Mykhailo Pavliuk

Reputation: 25

How can I change view for controller dynamically using javafx weaver

I have following project structure: Structure of the project

DashboarMainController:

@Component
@FxmlView("/view/normal/dashboard-main.fxml")
public class DashboardMainController implements Initializable {

    ...some logic
}

@FxmlView can be attached only once and accepts only one String

Program should have three possible screen resolution (small, normal, large) to change dynamically in the program. So I'd like to use different predefined views for the same controller to manage.

Upvotes: 1

Views: 174

Answers (1)

jewelsea
jewelsea

Reputation: 159466

Presumably, you actually want to have a very different layout with different nodes and controls displayed in the different views, otherwise, you could just use the standard JavaFX sizing hints in layout panes to achieve what you want. So this is the assumption that this answer makes.

I don't know anything about "javafx weaver", but perhaps make the DashboardMainController abstract, then define three subclasses for the different views and put your @FXMLView annotation on them.

You have to be careful in doing that as you will now have three controller instances, each linking to different nodes. However, this is probably exactly what you want.

In JavaFX, the controllers are tied pretty strongly with the views as the FXMLLoader will instantiate the node hierarchy for the view and insert it into the controller. So each controller instance and its associated nodes will map to a different view of the same underlying model data.

To keep everything in synch you will need to separate the model from views/view-models. For an overview of how to do this, which is relatively advanced (e.g. not novice level), see:

Upvotes: 1

Related Questions