Home Tutorials Swt Create Tree in SWT
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Create Tree in SWT

Advertisement
SWT allows an advantage of creating a tree. To represent the data in the hierarchical way, the class Tree is used. It provides the hierarchy of items.

Create Tree in SWT

     

This section illustrates you how to create a tree.

SWT allows an advantage of creating a tree. To represent the data in the hierarchical way, the class Tree is used. It provides the hierarchy of items. 

The constructor of class Tree consists of shell and the style value. The class TreeItem represents the hierarchy of tree items in a tree by adding an item to the tree. Its constructor consists of parent, style value and the index.

The method setText() sets the text specified. 

Here is the code of CreateTree.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;

public class CreateTree {  
  Display display;
  Shell shell;
  CreateTree()  {
  display= new Display();
  shell = new Shell(display);
  shell.setSize(230,220);
  shell.setText("Tree");
  shell.setLayout(new FillLayout());

  Tree tree = new Tree(shell, SWT.SINGLE | SWT.BORDER);
  TreeItem item1 = new TreeItem(tree, SWT.NONE, 0);
  item1.setText("Task1");
    TreeItem item1a = new TreeItem(item1, SWT.NONE, 0);
  item1a.setText("Task 1.1");
    TreeItem item1b = new TreeItem(item1, SWT.NONE, 1);
  item1b.setText("Task 1.2");

  TreeItem item2 = new TreeItem(tree, SWT.NONE, 1);
  item2.setText("Task2");
    TreeItem item2a = new TreeItem(item2, SWT.NONE, 0);
  item2a.setText("Task 2.1");
  TreeItem item2a1 = new TreeItem(item2a, SWT.NONE, 0);
  item2a1.setText("Task 2.1.1");
  TreeItem item2a2 = new TreeItem(item2a, SWT.NONE, 1);
  item2a2.setText("Task 2.1.2");
  TreeItem item2b = new TreeItem(item2, SWT.NONE, 1);
  item2b.setText("Task 2.2");
  TreeItem item2c = new TreeItem(item2, SWT.NONE, 2);
  item2c.setText("Task 2.3");

  TreeItem item3 = new TreeItem(tree, SWT.NONE, 2);
  item3.setText("Task3");
    TreeItem item3a = new TreeItem(item3, SWT.NONE, 0);
  item3a.setText("Task 3.1");
    TreeItem item3b = new TreeItem(item3, SWT.NONE, 1);
  item3b.setText("Task 3.2");

  shell.open();
  while(!shell.isDisposed()){
  if(!display.readAndDispatch())
  display.sleep();
  }
  display.dispose();
  }
  public static void main(String[] argv){
  new CreateTree();
  }
}

Output will be displayed as

Download Source Code

     

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 3, 2008

Ask Questions?    Discuss: Create Tree in SWT  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com