Reputation: 303
I tried a few methods:
tabPane.getSelectionModel().selectedItemProperty().addListener((e, o, n)
if (tabPane.getTabs().isEmpty()) someButton.setDisable(false);
and this when creating the tab:
tab.setOnCloseRequest(e ->
if (tabPane.getTabs().isEmpty()) someButton.setDisable(false);
But both are not working. The second method is definitly wrong as it is checking if there are tabs before actually closing the tab. Any solutions? Many thanks
Upvotes: 1
Views: 663
Reputation: 159291
I'm not sure if the below is what you want, but you can check that the size of the matched tab list in the tabpane <= 1 rather than empty.
tab.setOnCloseRequest(event -> {
TabPane tabPane = tab.getTabPane();
if (tabPane.getTabs().size() <= 1) {
// don't allow the last tab to be closed.
event.consume();
return;
}
});
Consuming the close request will prevent closure, but you could do other work in that event as well or instead (such as manipulating the disable property of your button) if you wish.
Usually button disable properties are well controlled via a binding, so perhaps something like MBec's solution might be a good idea in that case if that is all you need to accomplish.
Upvotes: 2
Reputation: 2210
Create isNotEmpty BooleanBinding
on TabPane ObservableList<Tab>
.
TabPane tp = new TabPane(new Tab("A"),new Tab("B"));
final BooleanBinding empty = Bindings.isNotEmpty(tp.getTabs());
Button someButton = new Button();
someButton.disableProperty().bind(empty);
Upvotes: 3