Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Resizing JPanels  (Read 3074 times)
0 Members and 1 Guest are viewing this topic.
Offline philfrei
« Posted 2013-03-13 20:41:23 »

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!

music and music apps:
Offline deepthought
« Reply #1 - Posted 2013-03-14 02:49:30 »

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

Mad Scientist
Offline HeroesGraveDev

JGO Kernel

Medals: 383
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

« Reply #2 - Posted 2013-03-14 02:58:20 »

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

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2013-03-14 22:47:19 »

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.

music and music apps:
Offline BoBear2681

JGO Coder

Medals: 19

« Reply #4 - Posted 2013-03-15 04:03:03 »

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().
Offline philfrei
« Reply #5 - Posted 2013-03-15 19:46:25 »

Thank you!!  Grin

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.

music and music apps:
Pages: [1]
  ignore  |  Print  

Riven (847 views)
2019-09-04 15:33:17

hadezbladez (5791 views)
2018-11-16 13:46:03

hadezbladez (2603 views)
2018-11-16 13:41:33

hadezbladez (6207 views)
2018-11-16 13:35:35

hadezbladez (1499 views)
2018-11-16 13:32:03

EgonOlsen (4734 views)
2018-06-10 19:43:48

EgonOlsen (5792 views)
2018-06-10 19:43:44

EgonOlsen (3276 views)
2018-06-10 19:43:20

DesertCoockie (4175 views)
2018-05-13 18:23:11

nelsongames (5501 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!