0

I am writing code to create a tree in SWT RCP. In this tree, I want to implement a functionality - When I double on a node, The name of the node should be displayed. The wrote for that purpose is -

private void addDoubleClickListener() {
    // TODO Auto-generated method stub
    treeViewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent e) {
            ISelection selection = e.getSelection();
            if (selection instanceof IStructuredSelection) {
                Object item = ((IStructuredSelection) selection)
                        .getFirstElement();
                if (item == null) {
                    return;
                } else {
                    System.out.println(item.toString());
                }
            }
        }
    });

}

It is working fine . But, my problem is, the nodes in my tree are editable. So, after a node is edited, when I double click on the node, it still displays the old data. Is there any solution for that?

Thanks!

Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68

1 Answers1

0

I have solved this problem.

Just added this line -

System.out.println(tree.getSelection()[0].getText());

instead of

 System.out.println(item.toString());

Because while renaming the nodes, I was setting the text value to this node.

Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68
  • 2
    You should really be updating the data in your content provider when you edit the tree. Various event may make the tree viewer refresh the tree from the content provider and any changes to tree items will be lost. – greg-449 Sep 22 '15 at 09:20
  • Thanks @greg-449 - It happened just as you told. I used a content provider. But, I did not write anything inside inputChanged method(). Is this the method which is responsible for the data change? One more thing. I found that, when we explicitly call setInput() on the table viewer, then inputChanged() is called. But, here I am trying to change a single node. In that case how should I handle? Thanks again! – Srijani Ghosh Sep 22 '15 at 11:03
  • Update the data in the content provider and use `viewer.update(object)` to tell the viewer it has changed. Never set values in the TreeItem as the viewer has complete control of those. – greg-449 Sep 22 '15 at 11:56
  • Thanks @greg-449 I tried your solution. For some reason, it is not working. :(. May be I could not implement it in the proper way. Can you please help me? Should we continue in this thread or may be I can post a new question regarding that. Please let me know which ever you prefer. – Srijani Ghosh Sep 22 '15 at 12:22
  • Post a question showing how you are editing the tree – greg-449 Sep 22 '15 at 12:25
  • Thanks a lot @greg-449! – Srijani Ghosh Sep 22 '15 at 12:25
  • Posted a new Question @greg-449 . Please have a look. http://stackoverflow.com/questions/32717020/changes-not-reflecting-after-editing-tree-in-swt – Srijani Ghosh Sep 22 '15 at 12:36