Event Handling Flashcards
Event Handling
Flow passes user interaction back to the server side as component events. The application can handle these events in Java code using an event handler.
Event Listener
Lambda expressions are the easy way in Java to implement interfaces that only have a single method to implement, which is the case with event listeners.
Button button = new Button(“Click me!”,
event -> event.getSource().setText(“Clicked!!!”));
add(button);
Handler Methods
You can also direct events to methods with method references:
class ButtonBar extends HorizontalLayout {
public ButtonBar() {
add(new Button(“OK”, this::ok));
add(new Button(“Cancel”, this::cancel));
}
public void ok(ClickEvent<Button> event) { event.getSource().setText("OKed!"); } public void cancel(ClickEvent<Button> event) { event.getSource().setText("Canceled!"); } }
add(new ButtonBar());
Main View
The main view itself doesn’t have a route, as it’s only a frame for the actual content views.
A main view uses AppLayout by extending it and setting up the elements in the constructor.
Creating Header
The navigation bar of AppLayout is a horizontal element that can contain any component, such as a header or a horizontal menu.