Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Enable/Disable button on selection of ComboBox in the grid Vaadin 8

Gurwinder Singh
11 months ago Mar 16, 2021 9:47am
Olli Tietäväinen
11 months ago Mar 16, 2021 11:24am

Here's an example:

public class MyUI extends UI {

    public static class MyBean {
        public boolean isButtonEnabled() {
            return buttonEnabled;
        }
        public void setButtonEnabled(boolean buttonEnabled) {
            this.buttonEnabled = buttonEnabled;
        }
        public String getS() {
            return s;
        }
        public void setS(String s) {
            this.s = s;
        }
        private boolean buttonEnabled = true;
        private String s;
    }

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        Grid<MyBean> grid = new Grid<>();
        MyBean f1 = new MyBean();
        f1.setS("fff");
        MyBean f2 = new MyBean();
        f2.setS("uuu");
        grid.setItems(f1, f2);
        grid.addComponentColumn(myBean -> {
            ComboBox<String> comboBox = new ComboBox<>();
            comboBox.setItems("Item 1", "Item 2");
            comboBox.addValueChangeListener(e -> {
                myBean.setButtonEnabled(!myBean.isButtonEnabled());
                grid.getDataProvider().refreshItem(myBean);
            });
            return comboBox;
        });
        grid.addComponentColumn(myBean -> {
            Button button = new Button("Click me", e -> {
                Notification.show("You could click the Button");
            });
            button.setEnabled(myBean.isButtonEnabled());
            return button;
        });
        this.setContent(grid);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}
Gurwinder Singh
11 months ago Mar 16, 2021 2:01pm