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.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
How to get the Item value of an child item from TreeTable.
Hi,
I need the value from list which is the child element in TreeTable.
Here is my code:--------------------
//Code for addContainerProperty
TreeTable table = new TreeTable("TreeTable") ;
final HierarchicalContainer container = new HierarchicalContainer();
table.setContainerDataSource(container);
table.setImmediate(true);
container.addContainerProperty("billOfladingNo", String.class, "");
//Code to display the TreeTable in the View
table.setPageLength(15);
table.setVisibleColumns(
new Object {"containerNo"});
table.setColumnHeaders(
new String {"Container No."});
(Here we are using the addGeneratedColumn to apply the Color based on Container number we are getting for Table component, the code is working good but for TreeTable it is not working)
table.addGeneratedColumn("containerNo", new Table.ColumnGenerator() {
@Override
public Component generateCell(final Table source, final Object itemId, final Object columnId) {
//Trail 1
String containerNo = item.getItemProperty("containerNo").getValue().toString(); //This Line Throughs NullPointerException since item is null.
//Trial2
final HierarchicalContainer container = new HierarchicalContainer();
table.setContainerDataSource(container);
Item item1 = (Item) container.getParent(itemId);
container.getContainerProperty(itemId,"containerNo");//It returns an interger value
//Trial3
Container dataSource = source.getContainerDataSource();
if (dataSource instanceof Container.Hierarchical) {
Container.Hierarchical h1 = (Container.Hierarchical) dataSource;
System.out.println("child::::::" + h1.getChildren(1)); //This continously displays the first record value , but does not display the remaining records
return new Label();
}
});
Above are my 3 Trials but none of them works.
I need the value from list which is the child element in TreeTable.
Example:-
1A
Child1
2B
Child2
3C
Child3
So i need Child1 ,Child2 and Child3 to apply some logic but it always displays Child1.
(Can we get the same behaviour of addGeneratedColumn of Table into TreeTable)
Any Help is appreciated :)
Sorry it's a bit unclear to me what do you need. Can you describe what properties do you have in the hierarchical container and what is the generated property what do you need to generate.
Below i have attached the screenshot,
For Every Parent record i want to get the Child object using addGeneratedColumn.
In Table Component:
-----------------------
table.addGeneratedColumn("containerNo", new Table.ColumnGenerator() {
@Override
public Object generateCell(Table source, Object itemId, Object columnId) {
Item item = source.getItem(itemId);
String containerNo = item.getItemProperty("containerNo").getValue().toString();
// From the above line will get the value but in TreeTable Component we gettting ClassCastException because it's returing the Integer not the Object
What is the type of containerNo property when you add it to the HierarchicalContainer?
It's String Type,
Example:
container.addContainerProperty("containerNo", String.class, "");
Does something like this work:
BeanItemContainer<MyPojo> items = new BeanItemContainer<>(MyPojo.class);
ContainerHierarchicalWrapper container = new ContainerHierarchicalWrapper(items);
container.addItem(myPojo);
container.addItem(myPojo2);
container.setParent(myPojo2, myPojo);
TreeTable tree = new TreeTable("", container);
tree.addGeneratedColumn("containerNo", new ColumnGenerator() {
@Override
public Object generateCell(Table source, Object itemId, Object columnId) {
String containerNO = (String) source.getItem(itemId).getItemProperty("containerNo").getValue();
}
});
Thank you for immediate response for all.
Even we got the solution by the folloing code:
Property prop = source.getItem(itemId).getItemProperty("containerNo");if (prop != null && prop.getValue() != null) {String containerNo = prop.getValue().toString();}
But i have one more doubt,
ie By using TreeTable i am trying to display the ToolTips but it is not working, getting ClassCastException (java.lang.Integer cannot be cast to com.lone.business.HouseBillVO).
table.setItemDescriptionGenerator(new AbstractSelect.ItemDescriptionGenerator() {
public String generateDescription(Component component, Object itemId, Object propertyId) {
//Here propertyId is the columnName, itemId is the integer values of parent/child id's.
String tooltip = "";
//getting ClassCastException (java.lang.Integer cannot be cast to com.lone.business.HouseBillVO) for below lineHouseBillVO vo = (HouseBillVO) itemId;
if (propertyId != null && propertyId.toString().equalsIgnoreCase(STATUS)) {
tooltip = vo.getStatusToolTip();
}
if (propertyId != null && propertyId.toString().equalsIgnoreCase(START)) {
tooltip = vo.getStartTimeToolTip();
}
return tooltip;
}
});
Using ItemId we can get the required information for Table Component but for TreeTable Component it is not working.
Any solutions ?
What are the container types you are using with Table? BeanItemContainer perhaps? Are you using HierarchicalContainer for TreeTable? HierarchicalContainer item ids are integers. You can wrap BeanItemContainer with ContainerHierarchicalWrapper which allows you to have the same itemId behavior as in BeanItemContainer (id is the pojo itself).