How to execute an action when a tool window tab is closed?

Answered

I have a tool window with two content tabs added to it. I used the content manager to add the content to the tool window:

ContentManager contentManager = toolWindowEx.getContentManager();
Content content = contentManager.getFactory().createContent(new MyPanel(), "Panel 1", false);
content.putUserData(ToolWindow.SHOW_CONTENT_ICON, true);
content.setIcon(NoteStashIcons.AnIcon);
content.setCloseable(true);
contentManager.addContent(content);
contentManager.setSelectedContent(content);

I've set the content tab to be closable. Now I'm trying to figure out how to execute an action when the tab on the tool window is closed. Does anyone know the best way to do this?

2 comments
Comment actions Permalink

First option is to rely on Disposable/Disposer mechanism (if you need to release some tab-related resources):

content.setDisposer(Disposer.newDisposable { ... })


Second options is to use ContentManagerListener:

contentManager.addContentManagerListener(object : ContentManagerListener {
override fun contentRemoved(event: ContentManagerEvent) {
val removedContent = event.content
....
}
})


There's "contentManager.removeContent(content, __ dispose : Boolean __)" method, so these two options might behave differently.

1
Comment actions Permalink

Thanks for this. The Content manager listener solution was what I needed.

0

Please sign in to leave a comment.