Kamrul Hasan
Kamrul Hasan

Reputation: 69

How to bind Enum Values In vaadin flow?

In my vaadin flow project with springboot I faced a problem that enum values with combo box cannot be bind. I provided my code below. Anyone here who can help me out?

Combobox instantiation:

private ComboBox<Country> nationality = new CompoBox<>("Nationality");

Binding code:

binder.forField(nationality)
    .bind(Coach::getNationality,Coach:setNationality);

Upvotes: 3

Views: 3153

Answers (1)

Tulio
Tulio

Reputation: 432

I think you are missing the setItems call. Here is an example based on the Project Base for Vaadin Flow

package com.vaadin.starter.skeleton;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;

/**
 * The main view contains a button and a click listener.
 */
@Route("")
@PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {

    public MainView() {

        ComboBox<ENUM> comboBox = new ComboBox<ENUM>("Number");
        comboBox.setItems(ENUM.values());
        Binder<Bean> binder = new Binder<>();
        binder.setBean(new Bean());
        binder.bind(comboBox,Bean::getField,Bean::setField);
        Button button = new Button("Check value",
                        e-> Notification.show("Value in bean is " + binder.getBean().getField()));
        add(button,comboBox);
    }

    public enum ENUM {
        ONE,TWO,TREE
    }

    public static class Bean {
        private ENUM field;

        public ENUM getField() {

            return field;
        }

        public void setField(ENUM field) {

            this.field = field;
        }
    }
}

Upvotes: 9

Related Questions