SWT Tree Composite : Tree « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » SWT JFace Eclipse » TreeScreenshots 
SWT Tree Composite

/*
SWT/JFace in Action
GUI Design with Eclipse 3.0
Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic

ISBN: 1932394273

Publisher: Manning
*/


import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

public class Ch8TreeComposite extends Composite {
  public Ch8TreeComposite(Composite parent) {
    super(parent, SWT.NULL);
    populateControl();
  }

  protected void populateControl() {
    FillLayout compositeLayout = new FillLayout();
    setLayout(compositeLayout);

    int[] selectionStyle = SWT.SINGLE, SWT.MULTI };
    int[] checkStyle = SWT.NONE, SWT.CHECK };

    for (int selection = 0; selection < selectionStyle.length; selection++) {
      for (int check = 0; check < checkStyle.length; check++) {
        int style = selectionStyle[selection| checkStyle[check];
        createTreeViewer(style);
      }
    }
  }

  private void createTreeViewer(int style) {
    TreeViewer viewer = new TreeViewer(this, style);

    viewer.setContentProvider(new ITreeContentProvider() {
      public Object[] getChildren(Object parentElement) {
        return ((TreeNodeparentElement).getChildren().toArray();
      }

      public Object getParent(Object element) {
        return ((TreeNodeelement).getParent();
      }

      public boolean hasChildren(Object element) {
        return ((TreeNodeelement).getChildren().size() 0;
      }

      public Object[] getElements(Object inputElement) {
        return ((TreeNodeinputElement).getChildren().toArray();
      }

      public void dispose() {
      }

      public void inputChanged(Viewer viewer, Object oldInput,
          Object newInput) {
      }
    });

    viewer.setInput(getRootNode());
  }

  private TreeNode getRootNode() {
    TreeNode root = new TreeNode("root");
    root.addChild(new TreeNode("child 1").addChild(new TreeNode(
        "subchild 1")));
    root.addChild(new TreeNode("child 2").addChild(new TreeNode(
        "subchild 2").addChild(new TreeNode("grandchild 1"))));

    return root;
  }

}

class TreeNode {
  private String name;

  private List children = new ArrayList();

  private TreeNode parent;

  public TreeNode(String n) {
    name = n;
  }

  protected Object getParent() {
    return parent;
  }

  public TreeNode addChild(TreeNode child) {
    children.add(child);
    child.parent = this;
    return this;
  }

  public List getChildren() {
    return children;
  }

  public String toString() {
    return name;
  }
}

           
       
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.Tree Example 2
13.Demonstrates CheckboxTreeViewer
14.Demonstrates TreeViewerDemonstrates TreeViewer
15.SWT Tree SWT Tree
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.