I have created a program in which a TabPane contains multiple Tabs. When someone tries to close any Tab, I want to execute my own code: when a user clicks on the default close button for any Tab, it will ask for confirmation. If the user says "Yes" then the tab will be closed, otherwise it will remain open.
How can I do this?
I am doing something like below. but the Tab is still getting closed. How would I consume that Tab?
Tab tab = new Tab();
TabPane tabPane=new TabPane();
tabPane.getTabs().add(tab);
tab.setOnClosed(new EventHandler<Event>() {
@Override
public void handle(Event t) {
t.consume();
}
});
The
Tab
implementation for Java 8 has anonCloseRequest
property which allows you to prevent tab closing:Here's a potential solution. If you add your own "prompt the user" logic to the code below, it should do what you want.
I am getting my own way as given below.
I have created a hyperlink and set it as graphic for that Tab and its work fine for me.
Change to
onMousePressed
instead ofsetOnMouseReleased
,And also the close triggers to
onMousePressed
instead ofOnMouseReleased
.