Tree Example 2 : Tree « SWT JFace Eclipse « Java






Tree Example 2

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class TreeShellExample2 {
  Display d;

  Shell s;

  TreeShellExample2() {
    d = new Display();
    s = new Shell(d);

    s.setSize(250, 200);
    
    s.setText("A Tree Shell Example");
    s.setLayout(new FillLayout(SWT.HORIZONTAL));
    final Tree t = new Tree(s, SWT.SINGLE | SWT.BORDER);
    final TreeItem child1 = new TreeItem(t, SWT.NONE, 0);
    child1.setText("1");
    child1.setImage(new Image(d, "c:\\icons\\folder.gif"));
    final TreeItem child2 = new TreeItem(t, SWT.NONE, 1);
    child2.setText("2");
    child2.setImage(new Image(d, "c:\\icons\\folder.gif"));
    final TreeItem child2a = new TreeItem(child2, SWT.NONE, 0);
    child2a.setText("2A");
    final TreeItem child2b = new TreeItem(child2, SWT.NONE, 1);
    child2b.setText("2B");
    final TreeItem child3 = new TreeItem(t, SWT.NONE, 2);
    child3.setText("3");
    child3.setImage(new Image(d, "c:\\icons\\folder.gif"));

    final List l = new List(s, SWT.BORDER | SWT.SINGLE);

    t.addTreeListener(new TreeListener() {
      public void treeExpanded(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
        ti.setImage(new Image(d, "c:\\icons\\open.gif"));
      }

      public void treeCollapsed(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
        ti.setImage(new Image(d, "c:\\icons\\folder.gif"));
      }
    });

    t.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        TreeItem ti = (TreeItem) e.item;
        populateList(ti.getText());
      }

      private void populateList(String type) {
        if (type.equals("1")) {
          l.removeAll();
          l.add("File 1");
          l.add("File 2");
        }
        if (type.equals("2")) {
          l.removeAll();
        }
        if (type.equals("2A")) {
          l.removeAll();
          l.add("File 3");
          l.add("File 4");
        }
        if (type.equals("2B")) {
          l.removeAll();
          l.add("File 5");
          l.add("File 6");
        }
        if (type.equals("3")) {
          l.removeAll();
          l.add("File 7");
          l.add("File 8");
        }

      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }

  public static void main(String[] argv) {
    new TreeShellExample2();
  }
}

           
       








Related examples in the same category

1.SWT Tree With Multi columnsSWT Tree With Multi columns
2.Detect Mouse Down In SWT Tree ItemDetect Mouse Down In SWT Tree Item
3.Limit selection to items that match a pattern in SWT TreeLimit selection to items that match a pattern in SWT Tree
4.SWT Tree Simple DemoSWT Tree Simple Demo
5.Category Tree
6.Simple TreeSimple Tree
7.Bookmark OrganizerBookmark Organizer
8.Displays a single-selection tree, a multi-selection tree, and a checkbox treeDisplays a single-selection tree, a multi-selection tree, and a checkbox tree
9.Demonstrates TableTreeDemonstrates TableTree
10.Demonstrates TreeEditorDemonstrates TreeEditor
11.Tree ExampleTree Example
12.Demonstrates CheckboxTreeViewer
13.Demonstrates TreeViewerDemonstrates TreeViewer
14.SWT Tree SWT Tree
15.SWT Tree Composite
16.Print selected items in a SWT treePrint selected items in a SWT tree
17.Insert a SWT tree item (at an index)Insert a SWT tree item (at an index)
18.Detect a selection or check event in a tree (SWT.CHECK)Detect a selection or check event in a tree (SWT.CHECK)
19.Create a SWT tree (lazy)Create a SWT tree (lazy)
20.Create a treeCreate a tree