How to add a JFrame to a ToolWindow?

I want to add a JFrame to a toolwindow. mytoolwindow content is a JPanel. I want to call pack() of JFrame thats why I need to use a JFrame.

 

public void addtotoolwindow(ToolWindow toolWindow){
title.setLayout(new FlowLayout(FlowLayout.CENTER));
myToolWindowContent.add(title);
JFrame frame = new JFrame("Results");
frame.setContentPane(myToolWindowContent);
frame.pack();

ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
Content content = contentFactory.createContent(frame, "", true);
toolWindow.getContentManager().addContent(content);
}

In the above code, the line, 

Content content = contentFactory.createContent(frame, "", true);

 is wrong because its 1st argument has to be a JComponent.

0
6 comments

Why do you want to use `JFrame.pack()` to layout component, if it's not going to be used as a part of this frame?
(And, likely, will be relayauted at any tool window resize.)

0
Avatar
Permanently deleted user

This question https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001348020-ToolWindow-incorrect-alignments- 

was posted by one of my friends working on the same project. Relevant to that issue we were asked to call pack() from JFrame to resolve it. Thank you!

0

Using JFrame is unlikely a good solution, if a solution at all.

Please, share complete snippet of code, that constructs toolwindow content.
Screenshot in the linked topic is cropped and lacks crucial details.

0
Avatar
Permanently deleted user

I want to add two boxes horizontally to the Jpanel named myToolWindowContent.

JPanel myToolWindowContent = new JPanel();
Box box = Box.createVerticalBox();
Box countermeasure_box = Box.createVerticalBox();
JLabel title = new JLabel();
myToolWindowContent.setLayout(new GridLayout(1,2));
myToolWindowContent.setAutoscrolls(true);
box.setBorder(BorderFactory.createLineBorder(JBColor.BLACK));
countermeasure_box.setBorder(BorderFactory.createLineBorder(JBColor.BLACK));

//After adding elements to the two boxes...

myToolWindowContent.add(box);
myToolWindowContent.add(countermeasure_box);
this.addtotoolwindow(toolWindow); //I defined this function

public void addtotoolwindow(ToolWindow toolWindow){
title.setLayout(new FlowLayout(FlowLayout.CENTER));
myToolWindowContent.add(title);
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
Content content = contentFactory.createContent(myToolWindowContent, "", true);
toolWindow.getContentManager().addContent(content);
}
You can see I always get an empty space in right side of the toolWindow.



0

>I want to add two boxes horizontally
But you're adding three components:

myToolWindowContent.add(box);
myToolWindowContent.add(countermeasure_box);
myToolWindowContent.add(title);

 

Quoting GridLayout JavaDoc:
>When both the number of rows and the number of columns have been set to non-zero values ... the number of columns specified is ignored. 
>Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout.
>So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.

0
Avatar
Permanently deleted user

It worked. Thanks a ton!

0

Please sign in to leave a comment.