Search code examples
javagwtcelltable

Showing Edit / Delete buttons for each row in a GWT CellTable?


I have a cell table showing some data. For each row, I want to have two columns which contain edit / delete buttons. When each button is clicked, it should be able to notify a listener which button was clicked (and preferably also be able to pass in the object that row is associated with).

How can I do this? Specifically, I know how to render a button, but how can I process the on-click event and pass in the object which the user clicked to edit or delete?


Solution

  • This is the standard approach:

    myTable.addCellPreviewHandler(new Handler<MyObject>() {
    
        @Override
        public void onCellPreview(CellPreviewEvent<MyObject> event) {
            if ("click".equals(event.getNativeEvent().getType())) {
                if (event.getColumn() == 0 || event.getColumn() == 1) {
                    MyObject object = event.getValue();
                    Window.alert("Column clicked: " + event.getColumn());
                }
            }
        }
    
    });
    

    This is a more efficient solution, because you only have one handler attached to a table, instead of trying to attach a handler to each button in each row.