问题
Using Grails 2.3.9 and Vaadin plugin 7.3.9
class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
VerticalLayout layout = new VerticalLayout()
layout.setMargin(true)
SignInForm signInForm = new SignInForm()
layout.addComponent(signInForm)
layout.setComponentAlignment(signInForm, Alignment.MIDDLE_CENTER)
layout.setSizeFull()
setContent(layout)
}
}
Custom component
class SignInForm extends CustomComponent {
Panel p = new Panel()
public SignInForm() {
p.setSizeUndefined()
Label label = new Label("test");
p.setContent(label);
setCompositionRoot(p);
}
}
This is how it looks:
How can I center the custom component horizontally?
回答1:
Place the custom component inside a vetical layout. Set the size of the custom component undefined. Set the size of the vertical layout full & align it to the centre.
public void init(VaadinRequest request) {
VerticalLayout vLayout = new VerticalLayout();
SignInComponent signInComponent = new SignInComponent();
vLayout.addComponent(signInComponent);
vLayout.setSizeFull();
vLayout.setComponentAlignment(signInComponent, Alignment.MIDDLE_CENTER);
this.setContent(vLayout);
class SignInComponent extends CustomComponent {
public SignInComponent() {
Panel p = new Panel();
p.setSizeUndefined();
Label label = new Label("test");
p.setContent(label);
this.setSizeUndefined();
this.setCompositionRoot(p);
}
}
OR
Use a panel instead of custom layout:
public void init(VaadinRequest request) {
VerticalLayout vLayout = new VerticalLayout();
SignInPanel signInPanel = new SignInPanel();
vLayout.addComponent(signInPanel);
vLayout.setSizeFull();
vLayout.setComponentAlignment(signInPanel, Alignment.MIDDLE_CENTER);
this.setContent(vLayout);
}
class SignInPanel extends Panel {
public SignInPanel() {
this.setSizeUndefined();
Label label = new Label("test");
this.setContent(label);
}
}
Code output for both:
回答2:
If someone need - How to center Custom Component with minimal UI class usage (Navigator component required):
The Component:
public class LoginPanel extends Panel {
private TextField loginField;
private PasswordField passField;
private Button signInBtn;
public LoginPanel(){
initComponents();
buildLoginPanel();
}
private void initComponents(){
<omitted>
}
public void buildLoginPanel(){
<omitted>
}
The View that uses Component:
public class LoginView extends VerticalLayout implements View {
private LoginPanel loginPanel;
@Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
}
public LoginView(LoginPanel loginPanel){
setSizeFull();
addComponent(loginPanel);
setComponentAlignment(loginPanel,Alignment.MIDDLE_CENTER);
}
}
UI entry point:
@Override
protected void init(VaadinRequest request) {
Navigator navi = new Navigator(UI.getCurrent(),this);
navi.addProvider(viewProvider);
navi.navigateTo("login");
}
P.S after doing this - your component will be centralized
P.S.S if someone doenst like to use Navigator for some point , just remove View everywhere and instead of navigator.navaigateTo(...)
use setContent(new LoginView())
or something simlar
Hope someone this will help
来源:https://stackoverflow.com/questions/28559028/vaadin-center-custom-component