Get Instance of Toolwindow

Answered

Is there a way to get the Instance of my Toolwindow?

MyToolWindowFactory instance = ServiceManager.getService(project, MyToolWindowFactory.class);

..doesnt work.

 

Any suggestions?

0
6 comments
ToolWindow window = ToolWindowManager.getInstance(project).getToolWindow(TOOL_WINDOW_ID);
0

 @Aleksey, the `window` I get from ToolWindowManager is of type `ToolWindowImpl`. But I don't want that, I want MY Implementation class, like `MyToolWindow`.

Right now, `window.getClass() = ToolWindowImpl.class`, but what I want is `window.getClass() = MyToolWindow.class`. How do I get this?

UPDATED: So what I want is not the `ToolWindow` but my `ToolWindowFactory`, so that I can update the JBTable in the ToolWindow.

0

I'm not sure, that it's possible.
What do you need it for?

0

I have a `JBTable` in the ToolWindow and I want to update that in the `AnAction.actionPerformed()` method. So I wanted to get a hold of my ToolWindowFactory, since I have the methods to update the JBTable in that class.

0

Probably, you're looking for `toolwindow.getContentManager().getContents()` method.
It will return all tabs in a ToolWindow, that can be added using `ContentManager.addContent`.

1

Hey thanks for that, I got it. I did as below:

The below code is in my AnAction.actionPerformed() :

final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(TOOL_WINDOW_ID); // this will give the toolWindow
final Content content = toolWindow.getContentManager().getContent(0); // this will give the first tab
final MyToolWindowPanel myToolWindowPanel = (MyToolWindowPanel) content.getComponent(); // this will give the MyToolWindowPanel reference

In my MyToolWindowFactory.createToolWindowContent() (which extends ToolWindowFactory) I put the below code:

final MyToolWindowPanel panel = new MyToolWindowPanel(toolWindow);
panel.init();

And finally, my MyToolWindowPanel which extends SimpleToolWindowPanel has the below code:

public MyToolWindowPanel(ToolWindow toolWindow) {
super(true, false);
final Content content = ContentFactory.SERVICE.getInstance().createContent(this, "", false);
toolWindow.getContentManager().addContent(content);
this.setContent(mContentPanel);
}

mContentPanel contains my JBTable. So now I can get a reference to MyToolWindowPanel anywhere I want, and all my JBTable update code is in MyToolWindowPanel. 

Perfect!

 

1

Please sign in to leave a comment.