Issue
I have been reading this stackoverflow post. And they say tabPane.getTabs().remove(tab)
will not trigger the listener on the event closing the tab which makes sense.
Therefore, I have applied their solution to my problem (I understand the principle, but not really the code), but my listener will never get called. Neither tabPane.getTabs().remove(tab)
nor closeTab(tab)
with the method closeTab
defined like so:
private void closeTab(Tab tab) {
EventHandler<Event> handler = tab.getOnClosed();
if (null != handler) {
handler.handle(null);
} else {
tab.getTabPane().getTabs().remove(tab);
}
}
call my listener. My listener is however called when I do close the tab with the mouse.
Here is my listener:
private static void addListenerOnClosingTab(Tab tab) {
tab.setOnCloseRequest(new EventHandler<Event>()
{
@Override
public void handle(Event arg0)
{
Util.loggerControllerDiagram.info("Tab " + tab.getText() + " closed.");
}
});
}
Solution
You might want to use getOnCloseRequest
instead of getOnClosed
in your method closeTab
:
EventHandler<Event> handler = tab.getOnCloseRequest();
handler.handle(null);
tab.getTabPane().getTabs().remove(tab);
Answered By - IHopeItWontBeAStupidQuestion
Answer Checked By - Clifford M. (JavaFixing Volunteer)