• Post Reply Bookmark Topic Watch Topic
  • New Topic

Automatically resizing JPanels when JFrame is resized by dragging.  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wish to use 2 JPanels in a JFrame, one large (on left), and one smaller (on right).
The panel on the right needs a fixed with, say 300 pixels.

I would like to be able to resize the JFrame by dragging on its edges (or maximising it), so that the JPanels also change there shape, but I'm not sure how to do this.

Originally I initially set the size of the panels, then added them to JFrame (with BorderLayout), and used pack(), then when resizing the JFrame, the panels just stayed the same size, which is not what I want.

I vagualy remember there being something like a fill method when using GridBag layout, but I'd rather avoid that layout. Can anyone help? Thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a recent thread at the sun forums has a couple of options

http://forums.sun.com/thread.jspa?threadID=5406232
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I almost got it to work...



And the RelativeLayout is here Relative Layout Class

The problem is, when I stretch the JFrame vertically, the components don't stretch that way, but it works correct horizontally.

Can anyone assist? Thanks
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its easily done with the RelativeLayout using:



Originally I initially set the size of the panels, then added them to JFrame (with BorderLayout), and used pack(), then when resizing the JFrame, the panels just stayed the same size, which is not what I want.


I find this hard to believe, since this is the default behaviour of the BorderLayout:


You might also want to add an EmptyBorder to the panel if you need space around the borders.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I forgot to do..

JPanel panel = new JPanel(new BorderLayout(10,10));

I just had

JPanel panel = new JPanel();

I thought the default Layout was BorderLayout, or is it Flow?

Anyway, it seems to work, thanks for help.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
colin shuker wrote:
I thought the default Layout was BorderLayout, or is it Flow?

Default for what? For JPanel, the default layout is FlowLayout. For a JFrame / contentPane or other root container's contentPane it's BorderLayout.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!