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.

addGeneratedColumn() - How to access this column?

Benny Bräuer
9 years ago Apr 19, 2012 9:14am
Johannes Häyry
9 years ago Apr 19, 2012 11:29am
Benny Bräuer
9 years ago Apr 19, 2012 12:51pm
Charles Anthony
9 years ago Apr 19, 2012 2:18pm

Hi,

Here's a generic way of using a generated checkbox to keep track of selected items in a table. It'll work with SQLContainer, and any other container for that matter.

HTH,
Cheers,
Charles

final Table table = new Table();
    table.setSizeUndefined();

    /* Define the names and data types of columns.
     * The "default value" parameter is meaningless here. */
    table.addContainerProperty("First Name", String.class,  null);
    table.addContainerProperty("Last Name",  String.class,  null);
    table.addContainerProperty("Year",       Integer.class, null);

    /* Add a few items in the table. */
    table.addItem(new Object[] {"Nicolaus","Copernicus", 1473}, 1);
    table.addItem(new Object[] {"Tycho",   "Brahe", 1546}, 2);
    table.addItem(new Object[] {"Giordano","Bruno", 1548}, 3);
    table.addItem(new Object[] {"Galileo", "Galilei", 1564}, 4);
    table.addItem(new Object[] {"Johannes","Kepler", 1571}, 5);
    table.addItem(new Object[] {"Isaac",   "Newton", 1643}, 6);

    /* This set contains the ids of the "selected" items */
    final Set<Object> selectedItemIds = new HashSet<Object>();
    selectedItemIds.add(4); // We'll start off with #4 selected, just to show that it works

    /* This checkbox reflects the contents of the selectedItemIds set */ 
    table.addGeneratedColumn("selected", new Table.ColumnGenerator() {
      @Override
      public Object generateCell(Table source, final Object itemId, Object columnId) {
        boolean selected = selectedItemIds.contains(itemId);
        /* When the chekboc value changes, add/remove the itemId from the selectedItemIds set */
        final CheckBox cb = new CheckBox("", selected);
        cb.addListener(new Property.ValueChangeListener() {
          @Override
          public void valueChange(Property.ValueChangeEvent event) {
            if(selectedItemIds.contains(itemId)){
              selectedItemIds.remove(itemId);
            } else {
              selectedItemIds.add(itemId);
            }
          }
        });
        return cb;
      }
    });
    /* Just some cosmetics : no caption for the checkboc, and make it the first column */ 
    table.setColumnHeader("selected", "");
    table.setVisibleColumns(new String[]{"selected", "First Name", "Last Name", "Year"});
    layout.addComponent(table);
    
    /* Just a demo of how to use the selected items set */
    layout.addComponent(new Button("Delete", new Button.ClickListener() {
      @Override
      public void buttonClick(Button.ClickEvent event) {
        // Here, you'd do whatever it is you want to do : I'm just removing then from the table
        for (Object itemId : selectedItemIds) {
          table.removeItem(itemId);
        }
      }
    }));
Last updated on Apr, 19th 2012
Benny Bräuer
9 years ago Apr 20, 2012 7:40am
Jennifer Nam
8 years ago Jul 29, 2013 3:35pm
Altanmur Tumurbat
8 years ago Oct 15, 2013 10:29am
Charles Anthony
8 years ago Oct 15, 2013 10:55am
Altanmur Tumurbat
8 years ago Oct 15, 2013 11:11am
Altanmur Tumurbat
8 years ago Oct 16, 2013 12:27am
Charles Anthony
8 years ago Oct 16, 2013 7:31am
Altanmur Tumurbat
8 years ago Oct 16, 2013 9:10am
Charles Anthony
8 years ago Oct 16, 2013 9:30am
Altanmur Tumurbat
8 years ago Oct 16, 2013 9:32am
Altanmur Tumurbat
8 years ago Oct 16, 2013 10:21am
Charles Anthony
8 years ago Oct 16, 2013 10:49am
Charles Anthony
8 years ago Oct 16, 2013 11:10am

OK; I've changed the example above : the checkboxes now reflect the tables selection status, and vice versa. Toggling the checkbox changes the table's value and changing the table value updates the checkboxes.The inifinte loop is avoided by a simple boolean flag, and the checkboxes are kept track of by attach/detach listeners.

See the code as a gist on GitHub

Cheers,

Charles.

Altanmur Tumurbat
8 years ago Oct 16, 2013 11:58am
Leandro Hoffmann
8 years ago Oct 24, 2013 11:44am
Avdhesh Semwal
7 years ago Apr 18, 2014 7:20am
prasad kopparthi
4 years ago Apr 18, 2017 9:07pm