9

I've got the following code:

public Button getBtnSubmit(com.vaadin.ui.Button.ClickListener l) {
    if (null != l) {
        btnSubmit.addListener(l);
    }
    return btnSubmit;
}

public Table getTableCompany(HeaderClickListener hl) {
    if (null != hl) {
        tableCompany.addListener(hl);
    }
    return tableCompany;
}

I would like to add a listener that fires when I select a (different) row in the table.
This so that I can refresh some other controls with the table data, which listener should I use?

Johan
  • 74,508
  • 24
  • 191
  • 319

6 Answers6

16

addListener is deprecated now. Use the following instead.

table.addItemClickListener(new ItemClickEvent.ItemClickListener() {
    @Override
    public void itemClick(ItemClickEvent itemClickEvent) {
        System.out.println(itemClickEvent.getItemId().toString());
    }
});
Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
5

I would go for ItemClickListener:

 table.addListener(new ItemClickEvent.ItemClickListener() {

            @Override
            public void itemClick(ItemClickEvent event) {
               //implement your logic here
            }
        });

edit: For Vaadin 7+, use addItemClickListener method instead of addListener.

ogzd
  • 5,532
  • 2
  • 26
  • 27
  • If you wish to get the id of the row clicked on then `Integer value = (Integer) event.getItem().getItemProperty("id").getValue();` – Patton Feb 06 '13 at 03:50
  • 2
    what about `event.getItemId()` ? – ogzd Feb 06 '13 at 08:20
  • I believe itemId is different from id, I am speaking about a column that is hidden and whose value is used for processing at the backend. – Patton Feb 06 '13 at 08:29
  • 1
    This is now deprecated ib Vaadin 7, the answer from Ishan Thilina Somasiri is the correct one – Manish Patel Jun 02 '14 at 17:59
1

You want to add a ValueChangeListener

Charles Anthony
  • 3,155
  • 17
  • 21
1

If you use the ValueChangeListener don't forget to set

  table.setImmediate(true);

This means that the browser will report a change on selection immediately. If you don't set this your listener is not called.

Bruno Eberhard
  • 1,624
  • 16
  • 22
0

Read https://vaadin.com/book/-/page/components.table.html, section 5.15.1 "Selecting Items in a Table". You want to add a Property.ValueChangeListener.

herman
  • 11,740
  • 5
  • 47
  • 58
0

Many of these answers are both correct, and incorrect.

If you need to get the selected items in response to the click, register a ValueChangeListener. Calling getValue() to retrieve the selection from the ItemClickListener might be 1 item behind in a MultiSelect list. For example, the set of items won't include/exclude the item triggering the callback. You will not have a reference to the clicked item however.

If you simply want to respond to a click on an item, and do not need to consider the current selection state, register an ItemClickListener instead. This way you will know what item was actually clicked.

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78