Issue
In my application, using the spring & javafx-weaver bunch, I'm trying to create a table with drop-down fields org.controlsfx.control.table.TableRowExpanderColumn.
tableUser.setItems(usersObservableList);
tableColumn1.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getDepartment().getName()));
tableColumn2.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getLogin()));
tableColumn3.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getProfile().toStringFIO()));
TableRowExpanderColumn<User> tableExpanderColumn = new TableRowExpanderColumn<>(param -> {
FxControllerAndView<UserEditExpanderController, GridPane> userEditExpander = getFxWeaver().load(UserEditExpanderController.class);
GridPane nodeGridPane = userEditExpander.getView().orElse(new GridPane());
UserEditExpanderController userEditExpanderController = userEditExpander.getController();
userEditExpander.getController().setData(param.getValue());
return nodeGridPane;
}
tableUser.getColumns().add(0, tableExpanderColumn);
Initialization goes as it should, but when editing fields I understand, that one instance of the controller is used. That is, the one that was last initialized.
How do I get my own instance of the Controller for each View? How to use one Controller and View in a table many times?
As I understand it, getFxWeaver().load(UserEditExpanderController.class)
loads the same controller instance, but at the same time creating a new View.
Solution
You are right with your assumption, each load
operation instantiates a new view and wires it to the controller provided by the bean factory (Spring application context).
By default Spring components are singleton scoped, thus you always get the the same controller instance.
Given I understand your problem right, it can be fixed easily by switching to prototype scope:
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserEditExpanderController {
//...
}
Answered By - rgielen