PDA

View Full Version : RowLayout(Orientation.HORIZONTAL) not calculating height correctly



Clay Ferguson
31 Aug 2009, 9:16 AM
The following should show a horizontal array of buttons, but for some reason the height ends up being zero. I think RowLayout (horizontal) may have a bug so that it only renders right (nonzero height) if you manually set the height? Is this right or am I missing something?



public class HorizontalButtonBar extends LayoutContainer {

@Override
protected void onAfterLayout() {
super.onAfterLayout();
System.out.println("Debug Height = "+getHeight());
}

public HorizontalButtonBar(Button[] buttons) {
super(new RowLayout(Orientation.HORIZONTAL));
for (Button button : buttons) {
add(button, new RowData(-1,-1, new Margins(10,10,10,10)));
}
}
}

sven
31 Aug 2009, 9:21 AM
I guess you are not sizing your HorizontalButtonBar.

Clay Ferguson
31 Aug 2009, 11:10 AM
I guess you are not sizing your HorizontalButtonBar.

Shouldn't it work just like this works (below).... which works fine.




public class HorizontalButtonBar extends HorizontalPanel {


public HorizontalButtonBar(Button[] buttons) {
setSpacing(10);

for (Button button : buttons) {
add(button);
}
}


HorizontalPanel seems to realize that my buttons are not all zero in size, and therefore it looks fine. However RowLayout seems to forget that the content (my buttons) may have some size. I realize GXT is not trying to model an API like Swing where there there *is* always a 'preferredSize', but nonetheless it should work in a case as simple as this. its' just a row of buttons! I can't set the height, because I want to be 'font independent' in my code. Hard coding sizes (as any Swing or SWT user knows) is *extremely* bad practice, nearly 100% of the time.

Clay Ferguson
31 Aug 2009, 12:09 PM
here's what I did as a workaround...



public class HorizontalButtonBar extends LayoutContainer {
private Button[] buttons;
private static final BUTTON_BAR_SPACING = 10

@Override
protected void onAfterLayout() {
super.onAfterLayout();

if (buttons.length > 0) {
setHeight(buttons[0].getHeight()+BUTTON_BAR_SPACING*2);
}
}

public HorizontalButtonBar(Button[] buttons) {
super(new RowLayout(Orientation.HORIZONTAL));
this.buttons = buttons;

for (Button button : buttons) {
add(button, new RowData(-1,-1, new Margins(BUTTON_BAR_SPACING, BUTTON_BAR_SPACING, BUTTON_BAR_SPACING, 0)));
}
}
}


it is ugly that I had to override onAfterLayout...but at least it works. ;) This renders a horizontal array of buttons with *no space* at the left of the letftmost button which is what I wanted!

Arno.Nyhm
1 Sep 2009, 5:09 AM
thanks for your workaround.

im just now also getting grey hair with the layout: RowLayout / HBoxLayout / VBoxLayout.

in the API there is written that this layout not calculate the height and you have to set it by yourself or from the outer layout - but im not getting this working without setting some height like you writing this.

i need some widgets positioned horizontaly but i need the size of the H-Panel as the max size of the widgets.

JoeB
8 Sep 2009, 10:10 AM
For what it's worth, I've noticed the exact same problem - that RowLayout(horizontal) doesn't automatically calculate its height, whereas RowLayout(vertical) does. Sometimes I need to lay out a bunch of widgets horizontally, and I want to put them in a container whose height is automatically determined based on the contained widgets. I don't want to set a specific height on the container, because that's not maintainable when I put different widgets in it. This seems like a bug to me, or at least a painful limitation.

-- Joe

sven
8 Sep 2009, 10:20 AM
Horizontal RowLayout uses absolute positning. That means, that the holding container needs to be sized.

JoeB
8 Sep 2009, 10:34 AM
Okay, but is there any container that lays out its children horizontally and will automatically calculate its height? HBoxLayout doesn't seem to work this way either. Thanks for the tips!

sven
8 Sep 2009, 10:37 AM
This is more or less a html limitation. The only way to do that would be using float: left. But that brings other things that are not so good. ColumnLayout is doing that.

ljohnston
10 Dec 2009, 10:20 PM
Something like the following should get you what you're after:



ContentPanel c = new ContentPanel();
c.setHeaderVisible(false);
c.setBorders(false);
c.setBodyBorder(false);

HBoxLayout layout = new HBoxLayout();
layout.setPadding(new Padding(10,0,10,0));
layout.setHBoxLayoutAlign(HBoxLayout.HBoxLayoutAlign.MIDDLE);
c.setLayout(layout);

HBoxLayoutData data = new HBoxLayoutData(new Margins(0,5,0,0));
c.add(new Text("Label:"), data);

data = new HBoxLayoutData(new Margins(0,5,0,0));
data.setFlex(1);
c.add(new TextField(), data);

c.add(new Button("Button"));
Using ContentPanel for this seems like a pretty heavy-handed approach, but it does seem to work.

patniemeyer
26 May 2010, 1:00 PM
I got something working with a HorizontalPanel, but I had to wrap each element in a FitLayout in order to get them to use the available space... And the total width is broken somehow... I had to set the table width to match the parent width... If I set it to 100% it overflowed the parent.

There has to be some easier way to lay items out horizontally... I realize CSS is a mess, but can't we write a container with some logic to adjust the layout based on the space available? I have a feeling that I could have done so in less time than I spent trying to figure out how to use three different GXT layout, none of which worked as expected.

wwab
15 Nov 2012, 6:02 AM
was there any success?