Home Tutorials Swt Tree Example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Tree Example

Advertisement
This section illustrates you how to get the files from the available directories and create a tree.

Tree Example

     

This section illustrates you how to get the files from the available directories and create a tree.

SWT provides a class Tree to represent the data in the hierarchical way and TreeItem to represents the  hierarchy of tree items in a tree by adding an item to the tree.

In the given example, we are creating a tree which represents the hierarchical structure of available files in the directories. The class File performs all the operations of the files and directories. The method listRoots() provides the list of available file system roots. The method listFiles() returns an array of file paths. The method getName() returns the name of the file. In order to find whether the file is directory, we have used the method isDirectory().

The class TreeItem adds the item to the tree by the method setText() and setData(). The method getItems() returns an array of TreeItems. The method getData() returns the data of the particular file.

Here is the code of TreeExample.java

import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.TreeItem;

public class TreeExample {

  public static void main(String[] args) {
 int w,h;
 Display display = new Display();
 Shell shell = new Shell(display);
 shell.setText("Show Tree");
 shell.setLayout(new FillLayout());

 Tree tree = new Tree(shell, SWT.BORDER);
 File[] file = File.listRoots();
 for (int i = 0; i < file.length; i++) {
  TreeItem item = new TreeItem(tree, 0);
  item.setText(file[i].toString());
  item.setData(file[i]);
  new TreeItem(item, 0);
  }
  tree.addListener(SWT.Expand, new Listener() {
  public void handleEvent(Event event) {
 TreeItem item1 = (TreeItem) event.item;
  TreeItem[] items = item1.getItems();
  for (int i = 0; i < items.length; i++) {
  if (items[i].getData() != null)
  return;
  items[i].dispose();
  }
  File file = (File) item1.getData();
  File[] files = file.listFiles();
  if (files == null)
  return;
  for (int i = 0; i < files.length; i++) {
  TreeItem item2 = new TreeItem(item1, 0);
  item2.setText(files[i].getName());
  item2.setData(files[i]);
  if (files[i].isDirectory()) {
  new TreeItem(item2, 0);
  }
  }
  }
  });
  shell.setSize(200,250);
  shell.open();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
  display.sleep();
  }
  display.dispose();
  }
}

Output will be displayed as

Download Source Code

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Ask Questions?    Discuss: Tree Example   View All Comments

Post your Comment


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