• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple GridBagLayout setFill question  RSS feed

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello! quick swing question if anyone can help please!

if i have a JFrame, with one JPanel, and the JFrame has GridBag as a layout....

why doesn't .setFill(GridBagConstraints.BOTH) make the JPanel fill the entire JFrame? i'm pretty sure it's not a syntax error, so didn't provide a code sample, but can do.

regards,

nick

 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and you don't have anything else added to the frame? What did you use for the xPos an yPos and width and height constraints? Do you know about Cay Horstmann's GBC class?
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correct - it is the only thing added to the Frame, and I'm using Horstmann's class



maybe I'm confused, but I would've thought I should be able to make the JPanel fill its containing JFrame. but I can't seem to.

I've chosen this simple example because I think my confusion here underlies, or at least represents why I have some difficulty with multiple panels.

If I've only 1 'grid' or cell (which I assume I have) surely that cell is equal to the entire JFrame? if I add another panel at 1,0, I have two cells, and so on.

I've definitely got better with GBC/layout though!

Nick
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm pretty sure it's not a syntax error, so didn't provide a code sample


A SSCCE should be provide with EVERY question!!! That is minimal executable code that demonstrates the problem should be included. Until the problem is solved you don't know what is or isn't relevant to the problem.

The "fill" constraint is use to fill a component within its grid. The size of the grid is equal to the preferred size of the largest component in the grid.

In this case you only have a single grid so the component is just displayed at its preferred size.

If you want the grid to fill the frame, then you also need to specify the weightx/y constraints. This will tell the layout manager which components get the extra space.

Read the section from the Swing tutorial on How to Use GridBagLayout, especially the section on the wightx/y constraints.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!