How to use TabPane tabs and selection model in MVVM pattern in JavaFX?

107 Views Asked by At

I want to try to use TabPane in MVVM design. For this I need to separate TabPane tabs and selection model and make an unidirectional binding for tabs an bidirectional binding for selection model. So, I have

public class View {

    private final TabPane tabPane = new TabPane();

    private void initialize() {
        Bindings.bindContent(this.tabPane.getTabs(), this.getViewModel().getTabPaneTabs());
        this.tabPane.selectionModelProperty()
            .bindBidirectional(this.getViewModel().getTabPaneSelectionModel()); ?????
    }

    public ViewModel getViewModel() {
        return this.viewModel;
    }
    ...
}

public class ViewModel {

    private final ObservableList<Tab> tabPaneTabs = FXCollections.observableArrayList();

    private final SingleSelectionModel<Tab> tabPaneSelectionModel = new SingleSelectionModel<Tab>() {

        @Override
        protected Tab getModelItem(int index) {
            return tabPaneTabs.get(index);
        }

        @Override
        protected int getItemCount() {
            return tabPaneTabs.size();
        }
    };

    public ObservableList<Tab> getTabPaneTabs() {
        return tabPaneTabs;
    }

    public SingleSelectionModel<Tab> getTabPaneSelectionModel() {
        return tabPaneSelectionModel;
    }

    ....
}

I think my solution for tabs is correct (or I am wrong) but I can't find how to do binding with selection model. Could anyone help me?

0

There are 0 best solutions below