Updating a custom tool window's contents on action performed

Answered

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() {
bodyText.setText(DEFAULT_BODY);
}

public void createUIComponents(String title, String bodyText) {
this.title.setText(title);
this.bodyText.setText(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);
toolWindow.getContentManager().setSelectedContent(content);

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

Thank you in advance!

3 comments
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:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009671059-How-to-access-the-components-of-a-custom-Tool-Window-post-creation?page=1#community_comment_360002193539

and then edit your panel's content.

1
Comment actions Permalink

Hi Karol,

Thanks for the response! One thing I'm wondering is since I followed the factory class for the sample:
https://github.com/JetBrains/intellij-sdk-code-samples/blob/main/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindowFactory.java

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

@Override
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);
toolWindow.getContentManager().addContent(content);
}

However, for the forum link you provided:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009671059-How-to-access-the-components-of-a-custom-Tool-Window-post-creation?page=1#community_comment_360002193539

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.

0
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`. 

0

Please sign in to leave a comment.