Hello all, I am new to Vaadin. After reading the documents I tried to make a small and simple CRUD app with Vaadin. In my app, I have a HomeUI class that extends UI, within it, I made a VerticalLayout and set that as my root layout. Inside that root(VerticalLayout) I have a HorizontalLayout, within that HorizontalLayout I have a Grid and a FormLayout. Initially I leave all the sizes as default, after all is done I have Grid followed by the FormLayout, they were close together. This made sense because the root VerticalLayout has 100% width as default, while the inner HorizontalLayout has undefined width, and it expended to accommodate the Grid and FormLayout inside it. BUT, when I tried to addComponentsAndExpand the Grid before addComponent the FormLayout, the grid stretch the entire screen and the FormLayout was no where to be seen. The only way to stretch the Grid and still have the FormLayout is to call setSizeUndefined in the FormLayout. Why is this? Also, how does the Grid knows how much space it needs?
@SpringUI
public class HomeUI extends UI {
VerticalLayout root;
@Autowired
Content content;
@Override
protected void init(VaadinRequest request) {
setRootLayout();
}
private void setRootLayout() {
root = new VerticalLayout();
root.addComponent(content);
setContent(root);
}
}
@SpringComponent
public class Content extends HorizontalLayout {
@Autowired
private StudentRepo repo;
List<Student> studentList = new ArrayList<>();
Grid<Student> grid = new Grid<>(Student.class);
@PostConstruct
private void init() {
updateGrid();
}
public Content() {
grid.setColumns("firstName", "lastName", "email");
addComponentsAndExpand(grid);
addComponents(new Form());
}
private void updateGrid() {
studentList = repo.findAll();
grid.setItems(studentList);
}
}
@SpringComponent
public class Form extends FormLayout{
@Autowired
StudentRepo repo;
TextField firstName = new TextField("First Name");
TextField lastName = new TextField("Last Name");
TextField email = new TextField("Email");
Button save = new Button("Save");
Binder<Student> binder = new Binder<>(Student.class);
public Form() {
setCaption("Student Information");
setSizeUndefined();
setDefaultComponentAlignment(Alignment.MIDDLE_CENTER);
addComponents(firstName, lastName, email,save);
binder.bindInstanceFields(this);
save.addClickListener(event -> System.out.println(new Student(
firstName.getValue(),
lastName.getValue(),
email.getValue()
)));
}
}