Search code examples
javamysqllistenervaadinvaluechangelistener

Vaadin ValueChangeEvent getProperty for a specific table column


I'm using Vaadin and a MySQL DB. I visualized a table of my DB (only one column) as a Table and I want to click the table rows and retrieve the value of a specific column when a click happens on a single row.

//Database Connection Class
...
private SQLContainer catExtractionContent = null;
FreeformQuery catExtractionQuery = new FreeformQuery("Select name as HAUPTKATEGORIE from Category where cat_main is null", connectionPool);
catExtractionContent = new SQLContainer(catExtractionQuery);

//UI-Class
private Object mainCatname=null;
mainCatTable = new Table();
mainCatTable.setContainerDataSource(getDBConn().getcatExtractionContent()); //This retrieves the SQL Container from obove
mainCatTable.addListener(new ValueChangeListener() { 
        
        public void valueChange(ValueChangeEvent event) {
         mainCatname = event.getProperty().getValue();
         System.out.println(mainCatname);
        }
});

My Table contains Category names.

The System.out.println prints 1,2,3... when I click the first, the second and the third row of my Table in the browser.

How can I get the column "name" / "HAUPTKATEGORIE"?

I was trying the whole day but I was not successful.


Solution

  • you have to read from the containerproperty the content of the columns. For this you need the propertyId. in your case propably "name" or "HAUPTKATEGORIE". i am not sure how the SQLContainer works.

    mainCatTable.addValueChangeListener(new ValueChangeListener() {
    
        @Override
        public void valueChange(ValueChangeEvent event) {
    
            Property<Table> p = event.getProperty(); //property of valuechangeevent in this case the Table instance
            Object itemId = p.getValue(); //selected item in table. also known as "itemId"
    
            Property<?> containerPropertyName = mainCatTable.getContainerProperty(itemId, "HAUPTKATEGORIE");
            System.out.println("HAUPTKATEGORIE : " + containerPropertyName.getValue());
    
        }
    });