Tab consuming in TabPane on default closing


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();

tab.setOnClosed(new EventHandler<Event>() {
            public void handle(Event t) {
8/2/2018 8:54:45 AM

Accepted Answer

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.

Hyperlink hlink = new Hyperlink();
Image image = new Image(MyClass.class.getResourceAsStream("/images/close.png"));
hlink.setGraphic(new ImageView(image));
Tab tab = new Tab();
hlink.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
              //Do somthing
11/27/2013 5:18:14 AM

The Tab implementation for Java 8 has an onCloseRequest property which allows you to prevent tab closing:

/* The installed event handler can prevent tab closing 
   by consuming the received event. */
public void setOnCloseRequest(EventHandler<Event> value)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow