mari mari - 12 days ago 8
Java Question

How can l set the values of a column in vaadin table to link or button?

I need to convert values in column "anzahl" to links or buttons, and l am struggling with the structure of vaadin table. I am getting database values from mysql table name "SYSTEM" (within entity class System).

This is my table properties which l have defined:

private void initSystemTable() {
JPAContainer<System> systems = new SystemServiceDB().getAllSystems();
table_system.setSizeFull();
table_system.setContainerDataSource(systems);
table_system.setVisibleColumns(new Object[] { "softwarebezeichung", "version", "anzahl", "lizenzen" });
table_system.setColumnHeader("softwarebezeichung", "Softwarebezeichung");
table_system.setColumnHeader("version", "Version");
table_system.setColumnHeader("anzahl", "Anzahl");
table_system.setColumnHeader("lizenzen", "Vorhandene lizenzen");
table_system.setImmediate(true);
}


and class SystemServiceDB:

public class SystemServiceDB implements SystemService{

@Override
public JPAContainer<System> getAllSystems()
{
JPAContainer<System> systems = JPAContainerFactory.make(System.class, "help-pu");

return systems;
}
}


This is my output table:

enter image description here

I can't use
addContainerProperty
to set the column as l load these values from mysql, I can't find the way to solve this problem. I will appreciate if anyone can help me to solve this.

table_system.addContainerProperty("anzahl", Link.class, null);
table_system.addContainerProperty("anzahl", Button.class, null);

Answer

Use generated columns.

This is an example of a generated column with a button:

table.addGeneratedColumn("generated", new ColumnGenerator() {

        @Override
        public Component generateCell(Table source,
                    final Object itemId, Object columnId) {
            Button button = new Button("caption");
            //Listener for the button
            button.addClickListener(Event -> {
                //Your code
            });
            return button;
        }          
});