user1631306 user1631306 - 2 months ago 54
Java Question

How to make vaadin table cell click listener

I have vaadin table with some rows and columns in it. How can I make the cells to be clickable of 1 particular column?

Goal : Instead of mouse hover, I want to show the tool tip on the click of the cell of that 1 particular column. The cell contains text as label.

I am not posting any code as all I have is just table (which wont help anyone).

Answer Source

Currently is not possible to have a clickListener attached to a particular cell in a Grid, as adressed on this discussion. They told that cells are just representations of data, not elements, so they cannot be clicked.

Instead of that, you can add a component inside a cell like the addon LabelButton (github) or simply add a Button and style it to be borderless.

PD: As a bonus, I'm adding the code to add Button in Grid:

Vaadin 7

RendererClickListener clickListener = new RendererClickListener() {
    private static final long serialVersionUID = 1L;

    @Override
    public void click(RendererClickEvent event) {
        //Do your actions
    }
};
ButtonRenderer renderer = new ButtonRenderer(ownerClickListener, "");
grid.getColumn("columnName").setRenderer(renderer);

An alternative to this approach would be use a property generator like GeneratedPropertyContainer and use an addon like ComponentRenderer (here) to render buttons. An example here in other of my answers..

Vaadin 8.1

Vaadin 8.1 now has a built-in ComponentRenderer for use in Grid. See What’s New, first item, “Components in Grid”.

grid.addComponentColumn(person -> {
    Button button = new Button("Click me!");
    button.addClickListener(click ->
        Notification.show("Clicked: " + person.toString()));
    return button;
});
// make sure the buttons fit in the cells of the Grid
grid.setBodyRowHeight(40);

As the example showed in Grid documentation in Vaadin page