• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Automatically resizing JPanels when JFrame is resized by dragging.

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3279
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't MAKE me come back there with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic