When should the JavaFX tab be collected by the garbage collector?

I have TabPanewith Tabin it. I create a new Tabone by adding it to mine TabPane. Then I close the tab and programmatically delete it from TabPane.

Tabnot set to null. Is this the right behavior? How to clean / destroy this object Tab?

+4
source share
2 answers

The garbage collector does not destroy objects on which you still keep the link.

Assuming you have a local variable or field myTab, just assign

myTab = null;

. , .

+6

EventHandler, .

tab.setOnClosed(null);

:

// I make setOnClose handler in my constructor
// My set on close handler will close my project
tab.setOnClosed(new EventHandler<Event>() {
    @Override
    public void handle(Event t) {
        closeProject();
    }
});

// Somehow my setOnClose handler still exists after closing the tab

public void closeProject() {

// Setting the setOnClose handler to null fixes the garbage collection issue for me
tab.setOnClosed(null);

tab.setUserData(null);
tab = null;
}
0

All Articles