How to refresh Tool Window

Answered

Hi, 

I'm having a GUI related problem with custom Tool Windows. I have this simple code:

    private void createUIComponents() {
    panel = new JPanel();
    panel.add(new JLabel("hello"));

    panel.setVisible(true);
    } 

This works, however, I want to change it when the actionPerformed is called of the same class. My actionPerformed looks like this:

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
    JPanel newPanel = new JPanel();
    newPanel.add(new JLabel("world"));

    newPanel.revalidate();
    newPanel.repaint();
    newPanel.updateUI();

    newPanel.setVisible(true);
    panel = newPanel;
    }

When I run this in the debugger, the actionPerformed runs, however the tool window doesn't update. I looked in toolWindow for some kind of refresh method but found none. Any help would be great.

Also please may someone tell me how to set text as code when posting on here, thanks.

7 comments
Comment actions Permalink

Yes, existing layout from UI Designer cannot be updated after panel creation.
You can replace `JPanel panel` with `com.intellij.ui.components.panels.Wrapper` and use `Wrapper.setContent(JComponent)` method.

Or just update panel content:

panel.removeAll();
panel.add(new JLabel("world"));
panel.validate();
panel.repaint();


>Also please may someone tell me how to set text as code when posting on here, thanks.
There are "Paragraph Styles" button on the toolbar (you might need to use "Shift+Enter" to separate formatted text from the rest).

0
Comment actions Permalink

Thanks, I ran into some issues. Updating the panel content still leads to no changes and when replacing JPanel panel with the wrapper, the tool window doesn't initialize. I run into this error:

failed to init toolwindow MyToolWindowFactory

java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1093)
at java.awt.Container.add(Container.java:973)
at com.intellij.openapi.wm.impl.content.ToolWindowContentUi.ensureSelectedContentVisible(ToolWindowContentUi.java:222)
at com.intellij.openapi.wm.impl.content.ToolWindowContentUi.access$200(ToolWindowContentUi.java:51)
at com.intellij.openapi.wm.impl.content.ToolWindowContentUi$2.selectionChanged(ToolWindowContentUi.java:190)

This is my createUIComponents(), where wrapper is defined as Wrapper wrapper;

private void createUIComponents() {
wrapper = new Wrapper();
JPanel jPanel = new JPanel();

jPanel.add(new JLabel("hello"));
jPanel.setVisible(true);

wrapper.setContent(jPanel);
wrapper.setVisible(true);
}
0
Comment actions Permalink

Please, share complete code of toolwindow initialisation.

0
Comment actions Permalink

https://github.com/AlexsFabulousFlow/ToolWindowPanel

Trigger the change by right clicking in editor and selecting Say world.

The problem is currently, it wont work because the form expects a JPanel and receives a Wrapper. If you replace the ToolWindowClass with the following code to update the panel:

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.components.panels.Wrapper;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class ToolWindowClass extends AnAction {
JPanel panel;

public ToolWindowClass() {
}

ToolWindowClass(ToolWindow toolWindow) {
}

private void createUIComponents() {
panel = new JPanel();

panel.add(new JLabel("hello"));
panel.setVisible(true);
}

JPanel getContent() { return panel; }

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
panel.removeAll();
panel.add(new JLabel("world"));
panel.revalidate();
panel.updateUI();
panel.setVisible(true);
}
}

Then the window initializes, but it doesn't update.

 

0
Comment actions Permalink

Sorry for the late reply,

Mentioned repository is not available.
Was the issue resolved?

0
Comment actions Permalink

No problem, also sorry for the late reply :). The issue was resolved, I ended up using the intellij message bus which refreshes all the guis perfectly. It seems just changing the panel directly either doesn't work, or works with a delay. Thanks for the help.

0
Comment actions Permalink

Hi,

I'm having the same issue with a Tool Window that I need to refresh when adding additional fields after some event, but validate() and repaint() doesn't work. How could you fixed it with the message bus? Thanks!

0

Please sign in to leave a comment.