Updating a custom tool window's contents on action performed


Hello - I followed the tool window sample at https://github.com/JetBrains/intellij-sdk-code-samples/tree/main/tool_window to register and create a custom tool window.

On action invoke, what is the correct or best way for updating the contents of the tool window?

This is what I have for the panel as content for the tool window:

public class CustomPanel {
private static final String DEFAULT_BODY = "This is default body text.";
private JPanel rootPanel;
private JLabel title;
private JTextPane bodyText;
private JScrollBar scrollBar;

public CustomPanel() {

public void createUIComponents(String title, String bodyText) {

public JPanel getContent() {
return rootPanel;

On action performed, I tried doing this for updating the `title` and `bodyText` of the panel:

ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("CUSTOM_WINDOW_ID");
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
CustomPanel updatedPanel = new CustomPanel();
updatedPanel.createUIComponents("New Title", "New Body Text");
Content content = contentFactory.createContent(updatedPanel.getContent(), "DISPLAY_NAME", false);

But this doesn't seem to be the right approach.

Thank you in advance!

Comment actions Permalink

Hi Alison,

You can get your panel with:
ToolWindow.getContentManager().getContents() and for each content check if content.getComponent() is instance of your CustomPanel as described in:

and then edit your panel's content.

Comment actions Permalink

Hi Karol,

Thanks for the response! One thing I'm wondering is since I followed the factory class for the sample:

My factory class creates the tool window by calling the custom panel's `getContent` method:

public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
Content content = contentFactory.createContent(new CustomPanel().getContent(), "Custom Panel Title", false);

However, for the forum link you provided:

The answer assumes the custom tool window's content would be created like this:

Content content = contentFactory.createContent(new CustomPanel(), "Custom Panel Title", false);

And then `CustomPanel` would need to extend `JComponent`.

When I do it this way, the `DEFAULT_BODY` text bound to the CustomPanel's root panel ends up not appearing. I think this is due to how I implemented `CustomPanel` since the text components are bound to its root panel.

I'm wondering which way to create the custom tool window is actually more appropriate? The issue with the first way following the example is that I wanted to use my `CustomPanel#createUIComponents` method for updating the text fields on action performed. However, I won't be able to use the method unless I pass in `CustomPanel()` for content creation... and If I pass in `CustomPanel()` for content creation, the text seems to not appear due to being bound to a root panel.

Apologies if this is unclear! I'm also new to Swing so also apologies in advance if this is better answered by Swing knowledge.

Comment actions Permalink

As an update - from going over https://plugins.jetbrains.com/docs/intellij/tool-windows.html#contents-tabs again, I realized on action performed, I could create a new content tab each time for the tool window using the first approach and make all of the tabs closeable in the `plugin.xml`. 


Please sign in to leave a comment.