A single-selection tree : Tree « SWT « Java Tutorial






A single-selection tree
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class TreeSingleSelection {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("TreeExample");

    Tree tree = new Tree(shell, SWT.SINGLE | SWT.BORDER);

    // Turn off drawing to avoid flicker
    tree.setRedraw(false);

    for (int i = 0; i < 5; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText("Root Item " + i);

      for (int j = 0; j < 3; j++) {
        TreeItem child = new TreeItem(item, SWT.NONE);
        child.setText("Child Item " + i + " - " + j);

        for (int k = 0; k < 3; k++) {
          TreeItem grandChild = new TreeItem(child, SWT.NONE);
          grandChild.setText("Grandchild Item " + i + " - " + j + " - " + k);
        }
      }
    }
    tree.setRedraw(true);
      
    tree.setBounds(10,10,400,400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}








17.57.Tree
17.57.1.Trees
17.57.2.Create a TreeCreate a Tree
17.57.3.Populate tree lazilyPopulate tree lazily
17.57.4.Create Three Level TreeCreate Three Level Tree
17.57.5.Insert TreeItem to TreeInsert TreeItem to Tree
17.57.6.Add checkbox mark to the tree nodeAdd checkbox mark to the tree node
17.57.7.A multiselection treeA multiselection tree
17.57.8.A single-selection treeA single-selection tree