# Two issues using GridBagSizer

**URL:** <https://www.ruby-forum.com/t/two-issues-using-gridbagsizer/147513>\
**Category:** wxRuby\
**Created:** [September 2, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/two-issues-using-gridbagsizer/147513 "2008-09-02T16:47:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![carluva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@carluva](https://www.ruby-forum.com/u/carluva)\
**Post date:** [September 2, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/two-issues-using-gridbagsizer/147513/1 "2008-09-02T16:47:46Z")

</div>

I am having a couple of issues with sizing objects using GridBagSizer.

The first is that the GridBagSizer itself isn’t expanding to fill the  
area available to it. For reasons that will be more clear when I get to  
the second issue, the GridBagSizer actually has only one cell. When I  
add the panel sized with the GridBagSizer to the BoxSizer that holds it,  
I use the EXPAND flag, but it doesn’t expand to fill the entire  
horizontal area (see attached image, top half).

The second problem relates to the way I’m using the GridBagSizer. It  
contains only a single cell that has two separate panels in it. These  
panels are mutually exclusive (either one or the other is showing, or  
possibly neither, but never both), so I want them to occupy the same  
space, which works using the GridBagSizer. When I first executed the  
code, the contents of @add\_panel and @edit\_panel would both be squished  
into a very small area at the upper-left corner of the @option\_panel.  
At position 1 in the code, I added the following lines:

@option\_sizer.calc\_min  
@option\_sizer.recalc\_sizes

This fixed the problem for the @add\_panel only–@edit\_panel would still  
appear squished into the corner. I then moved the two lines to appear  
at both position 2 and position 3 in the code, but the problem persisted  
(see attached image, bottom half).

require ‘ui/add\_panel’  
require ‘ui/edit\_panel’  
include Wx

class DetailPanel \< Panel  
def initialize(parent)  
super parent

```
@box = StaticBox.new self, :label => 'Entry Detail View'
... # creating other frames
@option_panel = Panel.new self
@add_panel = AddEntryPanel.new @option_panel
@edit_panel = EditEntryPanel.new @option_panel

@main_sizer = BoxSizer.new VERTICAL
set_sizer @main_sizer
@box_sizer = StaticBoxSizer.new @box, VERTICAL
... # defining other sizers
@option_sizer = GridBagSizer.new
@option_panel.set_sizer @option_sizer

@main_sizer.add @box_layout, 1, EXPAND|LEFT|RIGHT, 1

... # adding other frames to @box_sizer
@box_sizer.add @option_panel, 1, EXPAND|TOP, 6

... # other sizers within @box_sizer

@option_sizer.add @add_panel, GBPosition.new(0, 0), GBSpan.new,

```

EXPAND  
@option\_sizer.add @edit\_panel, GBPosition.new(0, 0), GBSPan.new,  
EXPAND  
#position 1

```
... # assign event handlers
show
@add_panel.hide
@edit_panel.hide

```

end

def add\_button\_click(event)  
@edit\_panel.hide  
@add\_panel.show  
#position 2  
end

def edit\_button\_click(event)  
@add\_panel.hide  
@edit\_panel.show  
#position 3  
end  
end

ui/add\_panel.rb and ui/entry\_panel.rb contain the necessary class  
definitions for those AddPanel and EntryPanel, of course.

Any help (including complete overhauls if there’s a better way I should  
be doing this) is welcome.

Thanks,  
Doug G.

---

<div class="post-metadata">

**Author:** ![carluva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@carluva](https://www.ruby-forum.com/u/carluva)\
**Post date:** [September 2, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/two-issues-using-gridbagsizer/147513/2 "2008-09-02T18:08:45Z")

</div>

Hi

Doug G. wrote:

> The first is that the GridBagSizer itself isn’t expanding to fill the  
> area available to it. For reasons that will be more clear when I get to  
> the second issue, the GridBagSizer actually has only one cell. When I  
> add the panel sized with the GridBagSizer to the BoxSizer that holds it,  
> I use the EXPAND flag, but it doesn’t expand to fill the entire  
> horizontal area (see attached image, top half).

I couldn’t fully follow what was going on in your code, but hope I can  
help with a few suggestions:

- Try using one of the GUI designers (eg DialogBlocks) to test build  
your layout. They make it easy to swap and nest Sizers and you can  
immediately see the effects. From then you can go and look at the XML to  
see how the Sizers are flagged and nested.

- Use a com

> The second problem relates to the way I’m using the GridBagSizer. It  
> contains only a single cell that has two separate panels in it.  
> Why are you using GridBagSizer then? If all you want is a place to swap  
> two panels or widgets in and out of, a simple BoxSizer would do fine.  
> Have a look at Sizer#detach and Sizer#replace methods, in conjunction  
> with using Window#hide or Window#destroy on the window you’re removing.

> These  
> panels are mutually exclusive (either one or the other is showing, or  
> possibly neither, but never both), so I want them to occupy the same  
> space, which works using the GridBagSizer.  
> Ok, see suggestion above. Here’s a simple class which wraps multiple  
> controls, presenting only one at a time.

---

<div class="post-metadata">

**Author:** ![carluva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@carluva](https://www.ruby-forum.com/u/carluva)\
**Post date:** [September 2, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/two-issues-using-gridbagsizer/147513/3 "2008-09-02T18:11:40Z")

</div>

Sorry, hit send too early…

The example class you might look at is:  
[http://weft-qda.rubyforge.org/svn/trunk/weft-qda/lib/weft/wxgui/controls/multitype\_control.rb](http://weft-qda.rubyforge.org/svn/trunk/weft-qda/lib/weft/wxgui/controls/multitype_control.rb)

Note that when swapping items in and out of a Sizer, you should probably  
call layout() afterwards to readjust the relative spacing.

> The first is that the GridBagSizer itself isn’t expanding to fill the  
> area available to it. For reasons that will be more clear when I get to  
> the second issue, the GridBagSizer actually has only one cell. When I  
> add the panel sized with the GridBagSizer to the BoxSizer that holds it,  
> I use the EXPAND flag, but it doesn’t expand to fill the entire  
> horizontal area (see attached image, top half).

- As for the expansion, you probably want to check that all the inner  
widgets are also set to expand within their parent. So in the case of  
your short textcontrols in the upper pane, they may need to be told to  
expand to full width.

> ```
> @option_panel = Panel.new self
> 
> ```

Note that if a Window is the sole child of a managed Frame it will  
resize automatically to fill that Frame. You don’t need to put it within  
a sizer.

hth  
alex

---

<div class="post-metadata">

**Author:** ![carluva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@carluva](https://www.ruby-forum.com/u/carluva)\
**Post date:** [September 2, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/two-issues-using-gridbagsizer/147513/4 "2008-09-02T18:47:39Z")

</div>

Alex F. wrote:  
[snip]

> Why are you using GridBagSizer then? If all you want is a place to swap  
> two panels or widgets in and out of, a simple BoxSizer would do fine.  
> Have a look at Sizer#detach and Sizer#replace methods, in conjunction  
> with using Window#hide or Window#destroy on the window you’re removing.

[snip]

Alex,

Perfect! I used a BoxSizer and added detach calls to the button\_click  
event handlers. Hadn’t discovered the detach method previously. That  
solved both problems. Thanks for your help!

Doug
