Sorry if this has been asked before, but I can’t seem to find any posts that would show how to do this. I’m trying to place a login form in the middle-center (vertically and horizontally) of the page. This is what I have so far:
public class LoginWindow extends Window {
private Layout layout = new VerticalLayout();
private Button btnLogin = new Button("Login");
private TextField txtUsername = new TextField("Username");
private TextField txtPassword = new TextField("Password");
public LoginWindow() {
super(Constants.APPLICATION_NAME + " Login");
setName("login");
setContent(layout);
initUI();
}
private void initUI() {
txtUsername.setRequired(true);
txtPassword.setRequired(true);
txtPassword.setSecret(true);
final Panel loginPanel = new Panel("Login...");
layout.addComponent(loginPanel);
final FormLayout loginForm = new FormLayout();
loginForm.setMargin(true);
loginForm.setStyleName("loginForm");
loginForm.addComponent(txtUsername);
loginForm.addComponent(txtPassword);
loginForm.addComponent(btnLogin);
loginPanel.setContent(loginForm);
}
}
How could I make my loginForm appear in the center-middle of the page?
private VerticalLayout layout = new VerticalLayout();
, so that you have the VerticalLayout API to call upon instead of the Layout interface. VerticalLayout has the method setComponentAlignment which is exactly what you need. You can now add anywhere, as long as it is after “layout.addComponent(loginPanel);” this code:
layout.setSizeFull();
layout.setComponentAlignment(loginPanel, Alignment.MIDDLE_CENTER);
and this will center the loginPanel! The layout has to be set “size full” so that it takes the whole browser window, so that you can center according to it.
The panel has 100% width, and so takes the whole browser width, which may look a little wierd too. I suggest you make it as tight as possible to make it look nicer. This is done with
loginPanel.setWidth(null);
Here’s the whole code once more. All my changes are that I added rows 24-26
public class LoginWindow extends Window {
private VerticalLayout layout = new VerticalLayout();
private Button btnLogin = new Button("Login");
private TextField txtUsername = new TextField("Username");
private TextField txtPassword = new TextField("Password");
public LoginWindow() {
super(Constants.APPLICATION_NAME + " Login");
setName("login");
setContent(layout);
initUI();
}
private void initUI() {
txtUsername.setRequired(true);
txtPassword.setRequired(true);
txtPassword.setSecret(true);
final Panel loginPanel = new Panel("Login...");
layout.addComponent(loginPanel);
layout.setSizeFull();
layout.setComponentAlignment(loginPanel, Alignment.MIDDLE_CENTER);
loginPanel.setWidth(null);
final FormLayout loginForm = new FormLayout();
loginForm.setMargin(true);
loginForm.setStyleName("loginForm");
loginForm.addComponent(txtUsername);
loginForm.addComponent(txtPassword);
loginForm.addComponent(btnLogin);
loginPanel.setContent(loginForm);
}
}