简体   繁体   中英

How to edit a tree node in SWT

I am creating a RCP application which will display a tree structure. I used the following code for this purpose. But, I need to make the nodes editable. How to do that? Please find the below code which I have written.

public class TreeView extends ViewPart {

    public static final String ID = "TreeProject.project";

    public TreeView() {
    }

    public static ProjectTree mc = new ProjectTree("root");
    public static TreeViewer treeViewer;

    @Override
    public void createPartControl(Composite parent) {

        Composite composite = new Composite(parent, SWT.NONE);
        treeViewer = new TreeViewer(composite);
        Tree tree = treeViewer.getTree();
        tree.setLocation(0, 0);
        tree.setSize(181, 469);

        StyledText styledText = new StyledText(composite, SWT.BORDER);
        styledText.setText("Welcome\"!");
        styledText.setBounds(179, 0, 415, 469);
        treeViewer.setContentProvider(new ProjectContentProvider());
        treeViewer.setInput(getRootNode());
        treeViewer.expandAll();

        System.out.println(tree.getSelection());
    }

    private ProjectTree getRootNode() {
        ProjectTree node0 = new ProjectTree("Node0");
        ProjectTree node1 = new ProjectTree("Node1");
        mc.addChild(node0, "");
        node0.addChild(node1, "");
        return mc;
    }

    @Override
    public void setFocus() {
    }
    }

You need to use a selection listener on the treeViewer which would give you the node selected. You then have to remove the node object and its children,if any, from the model . Here I see that your model is the object mc . Then call treeViewer.refresh() . Similarly for add .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM