Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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 823 times)
0 Members and 1 Guest are viewing this topic.
Offline philfrei
« Posted 2013-03-13 21: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!

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline deepthought
« Reply #1 - Posted 2013-03-14 03:49:30 »

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

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


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


« Reply #2 - Posted 2013-03-14 03: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 23: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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #4 - Posted 2013-03-15 05: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 20: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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (15 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!