Adding Items to the Tree

Items within the tree are represented by instances of the TreeItem class. You must create one TreeItem object for each item you wish to add to the tree.

How do I do that?

To attach an item directly to the trunk of the tree, you pass the TreeItem constructor the reference to the Tree object you have created, along with a style and the position in which you want the item to appear:

TreeItem child1 = new TreeItem(t, SWT.NONE, 0);
child1.setText("1");
TreeItem child2 = new TreeItem(t, SWT.NONE, 1);
child2.setText("2");
TreeItem child3 = new TreeItem(t, SWT.NONE, 2);
child3.setText("3");

This code creates three items, all directly attached to the trunk of the Tree.

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.