Create TreeView based on your own tree node structure : TreeViewer « SWT « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » SWT » TreeViewer 
17.62.4.Create TreeView based on your own tree node structure
import java.util.Vector;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
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.graphics.Image;
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 TreeViewBasedOnNode {
  static Display display = new Display();

  static Shell shell = new Shell(display);

  static final Tree tree = new Tree(shell, SWT.BORDER);

  static Vector nodes = new Vector();

  public static void traditional() {
    for (int i = 0; nodes != null && i < nodes.size(); i++) {
      Node node = (Nodenodes.elementAt(i);
      addNode(null, node);
    }
  }

  private static void addNode(TreeItem parentItem, Node node) {
    TreeItem item = null;
    if (parentItem == null)
      item = new TreeItem(tree, SWT.NONE);
    else
      item = new TreeItem(parentItem, SWT.NONE);

    item.setText(node.getName());

    Vector subs = node.getSubCategories();
    for (int i = 0; subs != null && i < subs.size(); i++)
      addNode(item, (Nodesubs.elementAt(i));
  }

  public static void main(String[] args) {
    Node category = new Node("A"null);
    nodes.add(category);

    category = new Node("a1", category);
    new Node("a11", category);
    new Node("a12", category);

    category = new Node("B"null);
    nodes.add(category);

    new Node("b1", category);
    new Node("b2", category);

    TreeViewer treeViewer = new TreeViewer(tree);

    treeViewer.setContentProvider(new MyTreeContentProvider());

    treeViewer.setLabelProvider(new MyLabelProvider());
    treeViewer.setInput(nodes);
    tree.setSize(300200);
    shell.setSize(300200);

    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

}
class MyLabelProvider implements ILabelProvider {
  public String getText(Object element) {
    return ((Nodeelement).getName();
  }

  public Image getImage(Object arg0) {
    return null;
  }

  public void addListener(ILabelProviderListener arg0) {
  }

  public void dispose() {
  }

  public boolean isLabelProperty(Object arg0, String arg1) {
    return false;
  }

  public void removeListener(ILabelProviderListener arg0) {
  }
}


class MyTreeContentProvider implements ITreeContentProvider{
  public Object[] getChildren(Object parentElement) {
    Vector subcats = ((NodeparentElement).getSubCategories();
    return subcats == null new Object[0: subcats.toArray();
  }

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

  public boolean hasChildren(Object element) {
    return ((Nodeelement).getSubCategories() != null;
  }

  public Object[] getElements(Object inputElement) {
    if (inputElement != null && inputElement instanceof Vector) {
      return ((VectorinputElement).toArray();
    }
    return new Object[0];
  }

  public void dispose() {
  }

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


class Node {
  private String name;

  private Vector subCategories;

  private Node parent;

  public Node(String name, Node parent) {
    this.name = name;
    this.parent = parent;
    if (parent != null)
      parent.addSubCategory(this);
  }

  public Vector getSubCategories() {
    return subCategories;
  }

  private void addSubCategory(Node subcategory) {
    if (subCategories == null)
      subCategories = new Vector();
    if (!subCategories.contains(subcategory))
      subCategories.add(subcategory);
  }

  public String getName() {
    return name;
  }

  public Node getParent() {
    return parent;
  }
}
17.62.TreeViewer
17.62.1.Tree Viewers
17.62.2.Using a TreeViewer
17.62.3.Create CheckBoxTreeView and react to CheckStateListener
17.62.4.Create TreeView based on your own tree node structure
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.