Tab consuming in TabPane on default closing


Question

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();
            }
        });
1
7
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));
hlink.setFocusTraversable(false);
Tab tab = new Tab();
tab.setGraphic(hlink);
hlink.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
              //Do somthing
         }
});
4
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
Icon