Jump to content

Quick question about GridBagLayout

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
I think I will be able to use this effectively, however, I'm struggling to understand how it works on the underneath.

From the tutorials I've seen, I understand that the GridBagLayout splits the area of the component into cells, and then you're able to use GridBagConstraints to define the width/height of the components that re going into the cells (i.e. how many cells that component will occupy), the padding of the compoents in the cells, how the cells are filled etc...

The thing I'm struggling to understand is: Does the GridBagLayout ALWAYS split the parent component into nine cells? This is the picture I've got in my head at the moment:
_____
|_|_|_|
|_|_|_|
|_|_|_|

For what I'm doing, i'm only going to need four cells, and although it's not likely to affect my project directly (I can just set the extra cells to have a weighting of zero), this seems very constricting.

I thought I'd ask, as I may have completely misunderstood, which would lead to terrible, terrible problems down the line.

Please and thank you!

~Fae

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I don' think so. At the gridbagconstraints you define in which row and column it is placed and dependant on what you place where it will dynamically create a grid i assume.

#3
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Right, I see... I did wonder. It seems that every example I've seen had the nine-grid as an example (of course, some components overlapped to show how width worked). It seemed silly to me to have a strict model like forcing a nine-grid upon us without giving us the option to define our own constraints.

That makes a lot more sense... And makes what I'm doing a lot simpler.

Thank you very much, Oxano ^_^

#4
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Did you try reading Sun´s tutorial on that? they usually are really good.

How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI with JFC/Swing > Laying Out Components Within a Container)

#5
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Of course, that was my first port of call. That's what first gave me the image in my head of the 3x3 grid. I can't remember exactly where on their site they had it, but they had an image of the gridbaglayout JFrame with buttons divided into nine squares with red lines, showing that you can have buttons that span multiple of these, but I just thought it was restricted to the default 3x3 fashion, and I thought that, as the programmer, I had to work around this to make my 2x2 grid.

Currently, I'm having problems with permissions, but I'll make a new thread for that. :)