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."});
 
[b]

(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)
[/b]
table.addGeneratedColumn(“containerNo”, new Table.ColumnGenerator() {
@Override
public Component generateCell(final Table source, final Object itemId, final Object columnId) {

          [b]

//Trail 1
[/b]
String containerNo = item.getItemProperty(“containerNo”).getValue().toString(); //This Line Throughs NullPointerException since item is null.

            [b]

//Trial2
[/b]
final HierarchicalContainer container = new HierarchicalContainer();
table.setContainerDataSource(container);
Item item1 = (Item) container.getParent(itemId);
container.getContainerProperty(itemId,“containerNo”);//It returns an interger value

             [b]

//Trial3
[/b]
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 :slight_smile:

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

24904.png

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 line
HouseBillVO 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).