# Problem centering a label

**URL:** <https://www.ruby-forum.com/t/problem-centering-a-label/163397>\
**Category:** wxRuby\
**Created:** [March 19, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397 "2009-03-19T22:09:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [March 19, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397/1 "2009-03-19T22:09:25Z")

</div>

Hi

I have frame.

Inside i want to put a static text as title, centered.

I use two sizers, one vertical an other horizontal.

But title is not centered, is put to left.

My code is :

require ‘wx’;

class TarefasNova \< Wx::Panel

```
def initialize(parent)

    rect = parent.get_client_rect();

    super( parent, 1011, Wx::Point.new( 0, 0 ), rect.get_size(),

```

SIMPLE\_BORDER, “NovaTarefa” );

```
    sizer = Wx::BoxSizer.new(Wx::VERTICAL );

    box = Wx::BoxSizer.new(Wx::HORIZONTAL);

    label = Wx::StaticText.new( self, :label => "CADASTRO DE TAREFAS

```

(NOVA)", :size =\> [250,-1] );

```
    box.add( label, 1, Wx::ALIGN_CENTER_VERTICAL, 5);

    sizer.add_item(box, 0, Wx::ALIGN_CENTER|Wx::ALL, 5);

 end

```

end

How to make my title centered in the panel?

Alejandro Michelin Salomon

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.

Virus Database (VPS): 090319-0, 19/03/2009  
Tested on: 19/03/2009 17:32:59  
avast! - copyright (c) 1988-2009 ALWIL Software.

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [March 19, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397/2 "2009-03-19T23:49:04Z")

</div>

Hi Alejandro,

you just need one sizer on your panel. Add one spacer  
(sizer.add\_stretch\_spacer) before and one after your text.

Like so:

```
sizer = BoxSizer.new HORIZONTAL
sizer.add_stretch_spacer
sizer.add StaticText.new(self, -1, 'asdf'), 0, 1
sizer.add_stretch_spacer

```

hth,  
Christian.

Alejandro Michelin salomon wrote:

> Hi
> 
> I have frame.
> 
> Inside i want to put a static text as title, centered.
> 
> I use two sizers, one vertical an other horizontal.
> 
> But title is not centered, is put to left.
> 
> My code is :
> 
> require ‘wx’;
> 
> class TarefasNova \< Wx::Panel
> 
> ```
> def initialize(parent)
> 
> rect = parent.get_client_rect();
> 
> super( parent, 1011, Wx::Point.new( 0, 0 ), rect.get_size(),
> 
> ```
> 
> SIMPLE\_BORDER, “NovaTarefa” );
> 
> ```
> sizer = Wx::BoxSizer.new(Wx::VERTICAL );
> 
> box = Wx::BoxSizer.new(Wx::HORIZONTAL);
> 
> label = Wx::StaticText.new( self, :label => "CADASTRO DE TAREFAS
> 
> ```
> 
> (NOVA)", :size =\> [250,-1] );
> 
> ```
> box.add( label, 1, Wx::ALIGN_CENTER_VERTICAL, 5);
> 
> sizer.add_item(box, 0, Wx::ALIGN_CENTER|Wx::ALL, 5);
> 
> end
> 
> ```
> 
> end
> 
> How to make my title centered in the panel?
> 
> Alejandro Michelin Salomon
> 
> * * *
> 
> avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.
> 
> Virus Database (VPS): 090319-0, 19/03/2009  
> Tested on: 19/03/2009 17:32:59  
> avast! - copyright (c) 1988-2009 ALWIL Software.

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [March 20, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397/3 "2009-03-20T13:42:53Z")

</div>

Christian :

I say in the other mail Â“I have a frameÂ”, brong i have a panel.

The panel is inside the main frame.

I change my code to :

class TarefasNova \< Wx::Panel

```
def initialize(parent)

    rect = parent.get_client_rect();

    super( parent, 1011, Wx::Point.new( 0, 0 ), rect.get_size(),

          SIMPLE_BORDER,

          "NovaTarefa" );

    #sizer = Wx::BoxSizer.new(Wx::VERTICAL)

    box = Wx::BoxSizer.new(Wx::HORIZONTAL);

    box.add_stretch_spacer();

    box.add Wx::StaticText.new(self, -1, 'asdf'), 0, 1

    #label = Wx::StaticText.new( self, :label => "CADASTRO DE 

```

TAREFAS  
(NOVA)", :size =\> [250,-1] );

```
    #box.add( label, 1, Wx::ALIGN_CENTER, 5);

    box.add_stretch_spacer();

    #sizer.add_item(box, 0, Wx::ALIGN_CENTER|Wx::ALL, 5);

end

```

end

But the text is not centered, the text is in the left.

Exist some diferences between frame and panel to user sizer?

De: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] Em nome de Christian Schmidt  
Enviada em: quinta-feira, 19 de marÃ§o de 2009 19:49  
Para: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Assunto: Re: [wxruby-users] Problem centering a label

Hi Alejandro,

you just need one sizer on your panel. Add one spacer  
(sizer.add\_stretch\_spacer) before and one after your text.

Like so:

```
sizer = BoxSizer.new HORIZONTAL
sizer.add_stretch_spacer
sizer.add StaticText.new(self, -1, 'asdf'), 0, 1
sizer.add_stretch_spacer

```

hth,  
Christian.

Alejandro Michelin salomon wrote:

> ```
> super( parent, 1011, Wx::Point.new( 0, 0 ), rect.get_size(),
> label = Wx::StaticText.new( self, :label => "CADASTRO DE TAREFAS
> end
> 
> ```
> 
> * * *
> 
> avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.
> 
> Virus Database (VPS): 090319-0, 19/03/2009  
> Tested on: 19/03/2009 17:32:59  
> avast! - copyright (c) 1988-2009 ALWIL Software.

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

* * *

wxruby-users mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Inbound message clean.

Virus Database (VPS): 090319-0, 19/03/2009  
Tested on: 20/03/2009 07:35:25  
avast! - copyright (c) 1988-2009 ALWIL Software.

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.

Virus Database (VPS): 090319-0, 19/03/2009  
Tested on: 20/03/2009 09:05:48  
avast! - copyright (c) 1988-2009 ALWIL Software.

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [March 20, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397/4 "2009-03-20T16:14:28Z")

</div>

Hi

Christian Schmidt wrote:

> ```
> super( parent, 1011, {:style => SIMPLE_BORDER, :name 
> 
> ```
> 
> =\>“NovaTarefa”} );
> 
> ```
> box = Wx::BoxSizer.new(Wx::HORIZONTAL);
> box.add_stretch_spacer();
> box.add Wx::StaticText.new(self, -1, 'asdf'), 0, 1
> box.add_stretch_spacer();
> 
> ```

Simpler, using StaticText ALIGN styles instead of extra spacers

box = Wx::VBoxSizer.new  
st = Wx::StaticText.new(self, :label =\> ‘adsf’, :style =\>  
Wx::ALIGN\_CENTRE)  
box.add st, 0, Wx::GROW

> Watch for the parents’ constructor - you needn’t set size and position.

This is a good point - use sizers to have children adjust to their  
parent’s size. Also, if a window is the only child of a frame parent  
(often the case with Panels inside Frames) then it will automatically  
take up all the avaiable space - no sizer needed.

a

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [March 20, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397/5 "2009-03-20T15:34:34Z")

</div>

Hi Alejandro,

this one actually works:

class TarefasNova \< Wx::Panel  
def initialize(parent)  
super( parent, 1011, {:style =\> SIMPLE\_BORDER, :name  
=\>“NovaTarefa”} );

```
  box = Wx::BoxSizer.new(Wx::HORIZONTAL);
  box.add_stretch_spacer();
  box.add Wx::StaticText.new(self, -1, 'asdf'), 0, 1
  box.add_stretch_spacer();

  set_sizer box
end

```

end

Watch for the parents’ constructor - you needn’t set size and position.  
Also never forget to call Panel#set\_sizer or your sizer won’t have any  
effect.

hth,  
Christian.

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [March 20, 2009, 4:55pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397/6 "2009-03-20T16:55:30Z")

</div>

Hi Alejandro,

I’m not sure to understand why you need 2 sizers but your main problem  
is that you haven’t told the panel to use the sizer !

Here is a sample code where there are 3 texts :

- at the top, the text is centered
- at the middle, the text is left aligned
- at the bottom, the text is right aligned

The important line is the one with self.sizer = box.  
You should also use wx constants to make your code clearer.

#!/usr/bin/env ruby

# wxRuby2 Sample Code. Copyright © 2004-2008 wxRuby development team

# Freely reusable code: see SAMPLES-LICENSE.TXT for details

begin  
require ‘rubygems’  
rescue LoadError  
end  
require ‘wx’

class MyPanel \< Wx::Panel  
def initialize(parent)  
super(parent)  
box = Wx::BoxSizer.new (Wx::VERTICAL)  
box.add\_item(Wx::StaticText.new(self, :label =\> “Centered Title”),  
:flag =\> Wx::ALIGN\_CENTER)  
box.add\_item(Wx::StaticText.new(self, :label =\> “Leftside Text”),  
:flag =\> Wx::ALIGN\_LEFT)  
box.add\_item(Wx::StaticText.new(self, :label =\> “Rightside Text”),  
:flag =\> Wx::ALIGN\_RIGHT)  
self.sizer = box  
end  
end

# This is the minimum code to start a WxRuby app - create a Frame, and

# show it.

Wx::App.run do  
frame = Wx::Frame.new(nil, :title =\> “Minimal wxRuby App”)  
panel = MyPanel.new(frame)  
frame.show  
end

Cheers.

Chauk-Mean.

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [March 25, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397/7 "2009-03-25T15:09:50Z")

</div>

Hi :

The problem is that, after call to create the child panel, i need to  
call  
send\_size\_event().

I find the solution becouse i minize the aplication to see the code in  
the  
editor and when i maxsimize the aplicattion the panel is ok filling all  
the  
frame space.

Alejandro

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.

Virus Database (VPS): 090324-0, 24/03/2009  
Tested on: 25/03/2009 11:01:09  
avast! - copyright (c) 1988-2009 ALWIL Software.

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [March 20, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/problem-centering-a-label/163397/8 "2009-03-20T18:14:43Z")

</div>

Chauk-Mean. :

Your code runs ok in a separate file.

If i call MyPanel from my menu to put the panel in my main frame, the  
code  
does not work.

This code :

begin

require ‘rubygems’

rescue LoadError

end

require ‘wx’;

class TarefasNova \< Wx::Panel

def initialize(parent)

```
super(parent)

box = Wx::BoxSizer.new (Wx::VERTICAL)

box.add_item(Wx::StaticText.new(self, :label => "Centered Title"),

```

:flag =\> Wx::ALIGN\_CENTER)

```
box.add_item(Wx::StaticText.new(self, :label => "Leftside Text"),

```

:flag =\> Wx::ALIGN\_LEFT)

```
box.add_item(Wx::StaticText.new(self, :label => "Rightside Text"),

```

:flag =\> Wx::ALIGN\_RIGHT)

```
self.sizer = box

```

end

end

The panel only shows Â“RighÂ”.

Does not size to fill frame.

When i put :

```
    rect = parent.get_client_rect();

    super( parent, 1011, Wx::Point.new( 0, 0 ), rect.get_size(),

```

SIMPLE\_BORDER, “NovaTarefa” );

This makes the panel fill frame.

The frame has menu bar and statusbar.

De: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] Em nome de Chauk-Mean P.  
Enviada em: sexta-feira, 20 de marÃ§o de 2009 10:39  
Para: General discussion of wxRuby  
Assunto: Re: [wxruby-users] RES: Problem centering a label

Hi Alejandro,

I’m not sure to understand why you need 2 sizers but your main problem  
is that you haven’t told the panel to use the sizer !

Here is a sample code where there are 3 texts :

- at the top, the text is centered
- at the middle, the text is left aligned
- at the bottom, the text is right aligned

The important line is the one with self.sizer = box.  
You should also use wx constants to make your code clearer.

#!/usr/bin/env ruby

# wxRuby2 Sample Code. Copyright (c) 2004-2008 wxRuby development team

# Freely reusable code: see SAMPLES-LICENSE.TXT for details

begin  
require ‘rubygems’  
rescue LoadError  
end  
require ‘wx’

class MyPanel \< Wx::Panel  
def initialize(parent)  
super(parent)  
box = Wx::BoxSizer.new (Wx::VERTICAL)  
box.add\_item(Wx::StaticText.new(self, :label =\> “Centered Title”),  
:flag =\> Wx::ALIGN\_CENTER)  
box.add\_item(Wx::StaticText.new(self, :label =\> “Leftside Text”),  
:flag =\> Wx::ALIGN\_LEFT)  
box.add\_item(Wx::StaticText.new(self, :label =\> “Rightside Text”),  
:flag =\> Wx::ALIGN\_RIGHT)  
self.sizer = box  
end  
end

# This is the minimum code to start a WxRuby app - create a Frame, and

# show it.

Wx::App.run do  
frame = Wx::Frame.new(nil, :title =\> “Minimal wxRuby App”)  
panel = MyPanel.new(frame)  
frame.show  
end

Cheers.

Chauk-Mean.

* * *

wxruby-users mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Inbound message clean.

Virus Database (VPS): 090319-0, 19/03/2009  
Tested on: 20/03/2009 13:13:39  
avast! - copyright (c) 1988-2009 ALWIL Software.

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.

Virus Database (VPS): 090319-0, 19/03/2009  
Tested on: 20/03/2009 13:56:02  
avast! - copyright (c) 1988-2009 ALWIL Software.
