I have a TabPane with lockable tabs. I want to fire a closed tab event when the user clicks a button in the contents of the tab. Here is the method that is called when the user clicks the button:
public class CustomTab extends Tab { ... protected void close() { Event.fireEvent(this, new Event(Tab.CLOSED_EVENT)); } .... }
I add this user tab to tabpane as:
TabPane tabPane = new TabPane(); ... CustomTab tab = new CustomTab(); tab.setOnClosed(new EventHandler<Event>() { @Override public void handle(Event t) { System.out.println("Closed!"); } }); tabPane.getTabs().add(tab); tabPane.getSelectionModel().select(tab);
You can usually close tabs by clicking the close icons (the default) in the tab header and "Closed!" printed on the screen. However, when the user clicks the button (that is, in the contents of the tab) and calls the close() method of CustomTab , "Closed!" Again. printed on the screen, but this time the tab does not close. Isn't that weird?
How to close a tab when clicking on an arbitrary button?
PS: tabPane.getTabs (). remove (tab) works, but firing the corresponding event is very elegant. It should also close the tab.
source share