Any way to center Button(s) in Form footer?

I have a Form (that’s in a Panel), and I’d like put one (or maybe more) buttons in the footer and have them (horizontally) centered. I’ve tried a bunch of different things with no luck, but I’m new to itmill.

Anybody have a way to do this?

Here is an example on how to do that:


        Form form = new Form();
        
        form.setWidth(null); // Form size grows with its content
        form.getLayout().setWidth(null); // Form layout size grows with its
                                             // content

        form.addField("field1", new TextField("My textfield"));

        HorizontalLayout footerLayout = new HorizontalLayout();
        footerLayout.setWidth("100%"); // Footer layout is as wide as the form

        Button button = new Button("A button");
        footerLayout.addComponent(button);
        footerLayout.setComponentAlignment(button, Alignment.TOP_CENTER);

        form.setFooter(footerLayout);

Some size modifications need to be done to get it the way you want (see the setWidth() calls). Failing to do this will cause invalid layouting and not the expected result. Invalid layouting can be detected using the “Analyze layouts” function in the debug window.

Yes, that worked, thanks.

And in case anyone wants to do the same thing, to center a set of buttons, I did this:


         Form form = new Form();
         form.setWidth(null); // Form size grows with its content
         form.getLayout().setWidth(null); // Form layout grows with content
 
         form.addField("field1", new TextField("My textfield"));
 
         HorizontalLayout footerLayout = new HorizontalLayout();
         footerLayout.setWidth("100%"); // Footer layout wide as the form

         HorizontalLayout buttons = new HorizontalLayout();
         buttons.addComponent(new Button("Ok"));
         buttons.addComponent(new Button("Cancel"));

         footerLayout.addComponent(buttons);
         footerLayout.setComponentAlignment(buttons, Alignment.TOP_CENTER);
 
         form.setFooter(footerLayout);