07_Event

Code-Dateien

DateinameAktion
CODECode_Bank.zipDownload
CODECode_Pizza.zipDownload
CODECode_Urlaub.zipDownload

Videos

DateinameAktion
VIDEOVideo_Bank_DAbspielen
VIDEOVideo_Pizza_EAbspielen
VIDEOVideo_Urlaub_DAbspielen

Lernmaterialien

Buttons

Remve all orders Add 10 orders
grid
VerticalLayout
├── HorizontalLayout
│   ├── Button ("Remove all orders")
│   └── Button ("Add 10 orders")
└── Grid

Properties

We need to access the button from multiple methods, so we store it as a property/field of the view.

public class UrlaubListe1  extends VerticalLayout {
    private final Button buttonRemoveAll = new Button("Remove all orders");
    private final Button buttonAdd10 = new Button("Add 10 orders");

Reason:

  • Field/property: when you need it later, for example to enable/disable it, change its text, or add listeners from another method.
  • Local variable: when it is only created and used once.

In Vaadin Flow, many developers keep important UI components like buttons, text fields, and grids as fields when the view has more logic.

The VerticalView is defined as local variable.

Add

        HorizontalLayout buttons = new HorizontalLayout(buttonRemoveAll, buttonAdd10);
        buttons.setSpacing(true);
        add(buttons);

Event

        buttonRemoveAll.addClickListener(b -> System.out.println("Remove ALL"));
        buttonAdd10.addClickListener(b -> System.out.println("Add 10"));
001.png

Skizze: View -> Button -> Event -> Method -> Service Method -> reload()

Event

OrdersView.java

        buttonRemoveAll.addClickListener(b -> removeAllOrders());
        buttonAdd10.addClickListener(b -> add10Orders());

OrdersView.java

    private void removeAllOrders() {
        pizzaOrderService.removeAll();
        buttonRemoveAll.setEnabled(false);
        reload();
    }

    private void add10Orders() {
        pizzaOrderService.fillTestData(10);
        buttonRemoveAll.setEnabled(true);
        reload();
    }

PizzaOrderService.java

    public void removeAll() {
        orders.clear();
    }

    public void fillTestData(int anz) {
        ...
        // orders.clear();    wrong!!!
        ...

GitHub

Copy code