Eclipse SWT Tree使用递归方法,实现Tree显示目录的显示
来源:互联网 发布:简单的数据分析软件 编辑:程序博客网 时间:2024/05/17 02:48
使用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() {@Overridepublic 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;}}}
- Eclipse SWT Tree使用递归方法,实现Tree显示目录的显示
- 使用递归的方式显示tree
- linux下使用tree命令显示目录的树结构
- win tree命令 tree导出目录 tree显示树形结构
- ubuntu tree 显示中文的方法
- linux tree命令--显示目录的树形结构
- linux tree命令--显示目录的树形结构
- Tree命令显示文本目录树
- CentOS之Tree显示目录结构-yellowcong
- linux命令--tree显示文件目录结构
- java SWT 开发,递归生成Tree
- 利用哈夫曼编码实现显示DOS的Tree
- swt tree spinner Group使用
- swt树形菜单tree使用
- SWT Tree
- SWT - Tree
- Python实现 Linux tree命令:用树形结构显示目录结构
- 用递归实现文件目录树状显示
- vc给别的程序窗体上的按钮发送消息
- 分区表及分区索引(15)--修改表分区属性和模板
- 这四个,你要求几个
- 课堂感受
- 分区表及分区索引(16)--增加和删除索引分区
- Eclipse SWT Tree使用递归方法,实现Tree显示目录的显示
- Windows时钟同步问题
- 分区表及分区索引(17)--其它索引分区管理操作
- (一)vmware安装-详细步骤
- 职场加薪步步高升的五大法则
- Spreadsheet::ParseExcel
- 模拟POST传参 并接收返回值
- vc 写win32控制台程序时如何隐藏自身的dos界面
- 在亚联的那些日子里