How do I know the preferred size of a JPanel that does not display according to its contents?

I use JPanel (with a few shortcuts inside) to add dynamic information to the chart. This panel is dynamically created, it is not visible before using it for drawing.

For this I use BufferedImage and I follow approximately the same steps as described in this other question . It works well if I specify all sizes (panel and its components).

As in the comments on the subject under discussion, how can I determine the optimal size of this panel? The same operation would be performed if this panel were displayed in the standard frame / layout setting.

In my case, how can I “package”, to some extent, this panel so that its size and the size of its contents are set to optimal (determined by the size of the labels)?

+4
source share
2 answers

Suraj and willcodejavaforfood put me on a good track.

Checking what is actually done in the pack() method, I see that it basically sets the current size to the one returned by getPreferredSize() .

From this, I managed to make such a solution:

 // Creating the panel JPanel lPanel = new JPanel(); //lPanel.setSize(1000, 1000); //default size, not needed anymore lPanel.setLayout(new BoxLayout(lPanel, BoxLayout.PAGE_AXIS)); //Adding the content lPanel.add(new JLabel("Blah")); // etc... //Adjust the panel to its preferred size lPanel.setSize(lPanel.getPreferredSize()); //Call the layout method //(this will adjust the content components to their correct size and position) lPanel.doLayout(); 

This method works correctly and adjusts the panel and its contents to the correct size (and answers my question in a simplified form: "How to find the preferred size? getPreferredSize() ").

However, this requires you to set the initial size to a size large enough for the content to enter, or they will not be placed in the layout. This is a little pathetic and not entirely “clean”, but so far I can’t find a way to avoid this.

Edit: Actually, the default size is not needed, because getPreferredSize() returns the correct value even before doLayout() called. Thus, the panel can be customized to the desired size before invoking the layout method.

+2
source

The direct answer is to call Window # pack () . This method will automatically set the size of all base child elements to preferred sizes (this depends on the layouts of the child containers, for example, BorderLayout do do damn about preffered sizes).

So, if you set the preferred sizes (or minimum / maximum sizes in case the layouts are similar to BorderLayout) of your child components, you need the pack () method.

[UPDATE]
One way is to add a HierarchyListener to your jpanel and check out the HierarchyEvent # DISPLAYABILITY_CHANGED event. This event is fired when your panel is implemented, which is ready for display (and the parent is available), at the moment you can do:

SwingUtilities#getWindowAncestor(myPanel).pack();

+1
source

Source: https://habr.com/ru/post/1300714/


All Articles