'smart gwt TreeGrid addSelectionUpdatedHandler can not get selected record

I have a treeGrid, and initialize the tree in the client. And add hander of addSelectionUpdatedHandler. The tricky thing is: it can get selected record in the first time. Then whenever I select the record in the next time, The SelectionUpdatedEvent is triggered, but the selected record is always null.

public void afterScreenLoad() {
 treeGrid = (TreeGrid) BaseWidget.getById(getApp().getScreenName() + "_ListGrid");
 ...
 addTreeGridHandler();
 addDataToGrids();
}

private void addDataToGrids() {
        Criteria criteria = treeGrid.getCriteria();

        tree = new Tree();
        root = new TreeNode();
        root.setIsFolder(true);
        tree.setRoot(root);

        jobs.forEach(job -> {
            TreeNode jobNode = new TreeNode();
            jobNode.setAttribute(ID, job.getId());
            jobNode.setAttribute(NAME, job.getVin());
            jobNode.setIsFolder(true);
            tree.add(jobNode, root);
            job.getProcessSteps().forEach(step -> {
                TreeNode stepNode = new TreeNode();
                stepNode.setAttribute(ID, step.getId());
                stepNode.setAttribute(PARENT, job.getId());
                stepNode.setAttribute(NAME, step.getName());
                tree.add(stepNode, jobNode);
            });
        });
        ListGridField[] fields = treeGrid.getAllFields();
        assetsDataSourceIndex += 1;
        ClientDataSource clientDataSource = new ClientDataSource(fields, assetsDataSourceIndex, true);
        clientDataSource.setCacheData(tree.getAllNodes());
        treeGrid.setDataSource(clientDataSource);
        treeGrid.fetchData();
        treeGrid.setFields(fields);
    }

   private void addTreeGridHandler() {
        getActionUtil().addHandler(treeGrid.addDrawHandler(event -> treeGrid.getBody()
                .setCanSelectText(true)));
        treeGrid.addDataArrivedHandler((DataArrivedHandler) dataArrivedEvent -> {
            SC.logWarn("Data arrived");
            if (dataLoaded && !selectionChanged) {
                selectPreviousSelectedAssetInTreeGrid();
            }
        });
        getActionUtil().addHandler(treeGrid.addSelectionUpdatedHandler(this::selectionChanged));
    }

    private void selectionChanged(BrowserEvent selectEvent) {
        SC.logWarn(selectEvent.getSource().toString());
        TreeNode selectedRecord = treeGrid.getSelectedRecord();
        boolean jobSelected = isJobSelected();
        SC.logWarn("Selected record existed: " + (selectedRecord != null));
        ...
   }

Could anybody help?



Solution 1:[1]

I tried to recreate your problem (because I couldn't run your code), but I didn't get the exact same behavior.
Instead the selected record is only null when deselecting a record in the TreeGrid (by ctrl-clicking it). Could that be something that happens the second time you click the record? What happens the third time you click it?

Perhaps you can recreate your problem in a smaller runnable example and post. Or you can start from my example and mimic your application to see when the problem occurs. Perhaps you'll even find out why it occurs.

Because the problem probably occurs because of something specific in your code, perhaps in the methods not shown by your example.

import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeNode;

public class Test {
  private Label selectedLbl;
  private TreeGrid treeGrid;


  public Test() {
    Tree tree = new Tree();
    tree.setRoot(new TreeNode("Tree", 
        new TreeNode("Foo",
            new TreeNode("1"),
            new TreeNode("2")),
        new TreeNode("Bar", 
            new TreeNode("3"),
            new TreeNode("4"))));
    
    treeGrid = new TreeGrid();
    treeGrid.setData(tree);
    treeGrid.addDrawHandler(event2 -> treeGrid.getBody().setCanSelectText(true));
    treeGrid.addSelectionUpdatedHandler(event2 -> {
      TreeNode selectedNode = treeGrid.getSelectedRecord();
      selectedLbl.setContents("Selected: " + (selectedNode == null ? "null" : selectedNode.getName()));
    });
    
    selectedLbl = new Label("Selected: ");
    
    Window win = new Window();
    win.addItem(treeGrid);
    win.addItem(selectedLbl);
    win.setSize("300", "300");
    win.show();
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lemoncactus