Quickz
Quickz

Reputation: 1836

JavaFX - How to close a Tab

Is there a method of some sort for closing a Tab in JavaFX? I'm looking for something similar to the one that's available to the Stage class (See the example 1).

So far the only known way to close a Tab through code seems to be by calling getOnCloseRequest or getOnClosed event manually and removing the Tab from it's TabPane (See the example 2).

Example 1:

private void closeWindow(Stage stage)
{
   stage.close();
}

Example 2

private void closeFirstTab(TabPane tabPane)
{
    Tab tab = tabPane.getTabs().get(0);
    EventHandler handler = tab.getOnCloseRequest();

    if (handler != null)
    {
        handler.handle(null);
    }
}

private void onClose(Event e, TabPane tabPane, Tab tab)
{
    if (e != null)
    {
        e.consume();
    }

    System.out.println("onClose");
    tabPane.getTabs().remove(tab);
}

Upvotes: 1

Views: 1613

Answers (1)

faris
faris

Reputation: 702

The tabPane.getTabs() method returns the collection of tabs in the TabPane. So if you call

tabPane.getTabs().remove(0)

or whichever index you choose, that should delete it from the stored tabs and stop it from displaying.

Upvotes: 2

Related Questions