Create my Own listener (TreeTable)


I would like to create my own listener in order to
detect when user Expand/Collapse columns
of my TreeTable.
I read it in the l
book of Vaadin
, however I don’t know how to start !
Could someone give me some help ?

Best regards.

No idea ?

There is no separate listener for this (you can listen to column resize events or reorder events), but you can override setColumnCollapsed(propertyId, collapsed) and there call your listeners after calling the superclass method.

my simple implementation for Table is as follows:

package cz.pse.tableconfigurator.component;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.tepi.filtertable.FilterTable;

import com.vaadin.ui.Component;

public class FilterTableWithCollapseEvent extends FilterTable {

    public FilterTableWithCollapseEvent() {

    public FilterTableWithCollapseEvent(String caption) {
    public void changeVariables(Object source, Map<String, Object> variables) {
        super.changeVariables(source, variables);

     * Handles the column collapse event sent by the client.
     * @param variables
    private void handleColumnCollapseEvent(Map<String, Object> variables) {
        if (variables.containsKey("collapsedcolumns")) {
            String[] collapsedCollumns  = (String[]
) variables.get("collapsedcolumns");
            Object[] visibleColumns = getVisibleColumns();
            for (int j = 0; j < visibleColumns.length; j++) {
                boolean isCollapsed = ArrayUtils.contains(collapsedCollumns , columnIdMap.key(visibleColumns[j]
            	if (isColumnCollapsed(visibleColumns[j]
) != isCollapsed){
                    fireEvent(new ColumnCollapseEvent(this, visibleColumns[j]
, isCollapsed));
    public void addColumnCollapseListener(ColumnCollapseListener listener){
        addListener("columnCollapse", ColumnCollapseEvent.class, listener,     ColumnCollapseEvent.COLUMN_COLLAPSE_METHOD);
    public void removeColumnCollapseListener(ColumnCollapseListener listener){
        removeListener("columnCollapse", ColumnCollapseEvent.class, listener);
    public static class ColumnCollapseEvent extends Component.Event {
        public static final Method COLUMN_COLLAPSE_METHOD;

        static {
            try {
                COLUMN_COLLAPSE_METHOD = ColumnCollapseListener.class
                                new Class[] { ColumnCollapseEvent.class });
            } catch (final java.lang.NoSuchMethodException e) {
                // This should never happen
                throw new java.lang.RuntimeException(e);

        private final boolean collapsed;
        private final Object columnPropertyId;

         * Constructor
         * @param source
         *            The source of the event
         * @param propertyId
         *            The columns property id
         * @param collapsed
         *            Indicates if colum was collapsed or elapsed
        public ColumnCollapseEvent(Component source, Object propertyId, boolean collapsed) {
            this.collapsed = collapsed;
            columnPropertyId = propertyId;

         * Get the column property id of the column that was resized.
         * @return The column property id
        public Object getPropertyId() {
            return columnPropertyId;
        public boolean getCollapsed(){
            return collapsed;
     * Interface for listening to column collapse events.
    public interface ColumnCollapseListener extends Serializable {

         * This method is triggered when the column has been collapsed or elapsed
         * @param event
         *            The event which contains the column property id and new collapse state
        public void columnCollapse(ColumnCollapseEvent event);



Thanks for your code.
That is exactly what I was looking for. :):slight_smile:



I am getting the event as many as collapsed columns.
However, I am interested in getting the collapsed column name via the event.

String collapsedCollumns contains ids.
I thought the Id can be used as an index to get the collapsed column name from visibleColumns.
However, that doesn’t match.

If you can think any way to get collaped column name, then please advise.

Hello Jennifer,
you can get this information from

  • getComponent()
    returns a table, when event has occured

  • getPropertyId()
    returns property ID of collapsed column (the one from

  • getCollapsed()
    , if column was collapsed and
    , if the collumn was expanded


Thank you for the reply, Agata.
I will try what you said above and will post the details later.

Thanks again. :slight_smile:

Hi Agita,

I think getPropertyId() returns visibleColumns (this is the full list of visible columns) rather than a property ID of collapsed column.

Please advise me how to update handleColumnCollapseEvent() to return a property ID of collapsed column (the one from visibleColumns) as you mentioned above.

Oh, I see. Please look at my edited post (!/thread/1634152/3502612


String collapsedCollumns = (String
) variables.get(“collapsedcolumns”);
Object visibleColumns = getVisibleColumns();

Suppose that I have an example to show ‘Field1, Field2, Field3, Field4’ as visibleColumns.
If I collapse Field1 and Field2, then I am getting [“1”,“2”]
in collapsedCollumns and I can get the column name “Field1” by visibleColumns[1]
and “Field2” by visibleColumns[2]
with no problem.

However, if I call Table.setColumnCollapsed(“Field1”, true) in my application during the initialization as a default and I collapsed Field2 from GUI screen manually, then I am getting [“3”, “4”]
in collapsedCollumns.
I don’t understand why property ids got changed when I call setColumnCollapsed() programatically.

Any idea?

Hello Jennifer,
I figured it out:
doesn’t contain indexes from

but those indexes can be obtain through
(see next version of post with code). But this change would fire the event, only if the collapsed state is changed from UI. To make it work, when calling the method programatically also,
setColumnCollapsed(Object propertyId, boolean collapsed)
should be overrided : public void setColumnCollapsed(Object propertyId, boolean collapsed) throws IllegalStateException { super.setColumnCollapsed(propertyId, collapsed); fireEvent(new ColumnCollapseEvent(this, propertyId, collapsed)); }
changeVariables(Object source, Map<String, Object> variables)

Hi Agata,

Overriding setColumnCollapsed is a perfect idea.
It solved my problems and issues.
It works for both GUI action and programatical calls.

Thank you so much for your help on this.

I am new to Vaadin, And having confusion while getting list(name or property Id) of collapsed column or columns which are not collapsed.
I have created a class which will override setColumnCollapsed as well as change variable
suppose class name is MyFilterTable extends FilterTable where i have written given code now I am creating FilterTable using above class >

MyFilterTable pagedFilterTable = new MyFiilterTable();

I don’t understand how to get Collapsed column here

Please advice me how to get Collpased column names.