Eclipse SWT Tree使用递归方法,实现Tree显示目录的显示

使用Eclipse 的SWT技术实现的一个将文件目录展示到SWT Tree中的功能,方法中使用的采用递归调用的形式,将文件目录中的所有层级的文件名称展示到树形目录中。

如果每一层目录采用一个循环遍历文件对象中的文件数组,这样只能遍历到最底层的循环代码中,始终不能遍历到最底层的文件中,所以采用了方法的递归调用进行逐层遍历的形式,方法执行结束也就是文件遍历到了最底层的时候。

废话少说,贴出实现的功能截图




实现代码:

package swt_jface.demo3;
import java.io.File;
import java.util.Vector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
public class TreeTest {

	//递归方法
	public static void listFiles(File rootFile, TreeItem item) {
		File[] files = rootFile.listFiles();
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				System.out.println(files[i].getName());
				TreeItem subItem = new TreeItem(item, SWT.None);
				subItem.setText(files[i].getName());
				listFiles(files[i], subItem);
			}
		}
	}
	
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));

		Vector<Category> categories = new Vector<Category>();
		Category category = new Category("Java libraries", null);
		categories.add(category);
		category = new Category("UI Toolkits", category);
		new Category("AWT", category);
		new Category("Swing", category);
		new Category("SWT/JFace", category);
		category = new Category("Java IDEs", null);
		categories.add(category);
		new Category("Eclipse", category);
		new Category("JBuilder", category);
		shell.setSize(300, 200);
		{
			Composite composite = new Composite(shell, SWT.NONE);
			composite.setLayout(new FillLayout(SWT.HORIZONTAL));
			{
				final Tree tree = new Tree(composite, SWT.BORDER
						| SWT.FULL_SELECTION);
				tree.addKeyListener(new KeyAdapter() {
					@Override
					public void keyPressed(KeyEvent e) {
						File file = new File("D:\\Workplace for SBD\\Workstation");
						File[] files = file.listFiles();
						tree.setItemCount(0);
						for (int i = 0; i < files.length; i++) {
							TreeItem item = new TreeItem(tree, SWT.NONE);
							item.setText(files[i].getName());
							//调用递归方法
							listFiles(files[i], item);
						}
					}
				});
				tree.setLinesVisible(true);
				tree.setHeaderVisible(true);
				{
					TreeColumn treeColumn = new TreeColumn(tree, SWT.NONE);
					treeColumn.setWidth(100);
					treeColumn.setText("New Column");
				}
			}
		}
		shell.open();

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

	static class Category {

		private String name;
		private Vector<Category> subCategories;
		private Category parent;

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

		public Vector<Category> getSubCategories() {
			return subCategories;
		}

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

		public String getName() {
			return name;
		}

		public Category getParent() {
			return parent;
		}
	}
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值