g0rdonL
g0rdonL

Reputation: 303

Check whether there are no tabs in TabPane on close JAVAFX

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

Answers (2)

jewelsea
jewelsea

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

MBec
MBec

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

Related Questions