Search code examples
gwtlayoutalignmentgxt

ExtGwt dynamically changing size of ContentPanel


While creating a small aplication in ExtGwt I've bumped into such problem:

Is there any way to center the content of the ContentPanel horizontally (and only horizontally) ?

I know the one solution is to use:

ContentPanel panel = new ContentPanel();
panel.setLayout(new CenterLayout());
//but here is the problem, I MUST set the size of panel
panel.setSize(400, 200);

However, I need this panel to have dinamically calculated height, because it has different content in various places.

Other thing is to use UIBinder, but that's somehow also not a good idea.

Anyone has idea how to achieve such solution?


Solution

  • layout the ContentPanel inside its container with a FitLayout. that way, the ContentPanel's size is always managed, and you won't have to specify it's dimensions.

    public class ContentPanelContainer extends LayoutContainer {
    
        @Override
        protected void onRender(Element parent, int index) {
            super.onRender(parent, index);
    
            setLayout(new FitLayout());
    
            ContentPanel panel = new ContentPanel();
            panel.setLayout(new CenterLayout());
    
            add(panel);
        }    
    }