Resizing JPanels

I have a program that can be run as both an application (uses JFrame) and an applet (uses JApplet).

Both display a JPanel that I call TopPanel which has several sub-components and a Border layout. Both display a JMenuBar (attached to the JFrame for the application, and to the JApplet for the applet).

One of the items in the menu bar allows a change of a configuration element and that would ideally include a resizing of the subpanel on TopPanel which hosts that element.

As far as I can tell, removing and adding that subpanel (also a JPanel) doesn’t result in a new display, even when the new subpanel has a new preferredSize(). The only way I’ve figured out to get the display to respond to the changes is to execute a pack() method at the JFrame.

pack(), however, is a method of Window, and JApplet is not a subclass. Is there an equivalent method to pack() that can be used for the applet form of this program?

Is there a better way to get the TopPanel (fills entire JFrame or JApplet) to redisplay?

I’m considering the possibility that there IS a simpler way but that I executed it incorrectly…

Thanks for help!

Applet has .resize(int,int) if that’s what you’re looking for. You could call that with the new preferred size.

Set the layout manager to null and just use setBounds()

It may require you to do the calculations yourself, but it works.

Thanks for the suggestions!

@HeroesGraveDev - I had just gone through a lot of trouble getting my rather elaborate JPanel to use an actual layout rather than null layout, and I really didn’t want to “go backwards”! Otherwise, it was a reasonable suggestion, especially for simpler screen layouts.

.reSize(int, int) did work for the JApplet! I found that .reSize(int, int) also works for JFrame, but it is deprecated in favor of .setSize(int, int). So I may end up using .setSize(int, int) for both.

The nice thing is that both take into consideration the new .preferredSize property and do all the messy calculating needed to revise/compensate the other components in the display.

I’m not 100% clear on what you’re doing, but if you’re adding and removing components at runtime, and want to do things the “right” way (e.g. have components laid out according to the installed LayoutManager), you typically just call revalidate().

Thank you!! ;D

So, .revalidate() is called on the component that had its property changed, not the container.

I was having trouble finding this method because I was stuck on the notion that the “reformatting” activity centered on the container.