//给你一个例子/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */ import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; import org.eclipse.swt.SWT; import java.io.File; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Event;public class Test { public Test() { } public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Lazy Tree"); Tree tree = new Tree(shell, SWT.BORDER); /* Initialize the roots of the tree */ File[] roots = File.listRoots(); for (int i = 0; i < roots.length; i++) { TreeItem root = new TreeItem(tree, SWT.NULL); root.setText(roots[i].toString()); root.setData(roots[i]); /* Use a dummy item to force the '+' */ new TreeItem(root, SWT.NULL); } /* Use SWT.Expand to lazily fill the tree */ tree.addListener(SWT.Expand, new Listener() { public void handleEvent(Event event) { /* * If the item does not contain a * dummy node, return. A dummy item * is a single child of the root that * does not have any application data. */ TreeItem root = (TreeItem) event.item; TreeItem[] items = root.getItems(); if (items.length != 1) return; if (items[0].getData() != null) return; items[0].dispose(); /* Create the item children */ File file = (File) root.getData(); File[] files = file.listFiles(); if (files == null) return; for (int i = 0; i < files.length; i++) { TreeItem item =new TreeItem(root,SWT.NULL); item.setText(files[i].getName()); item.setData(files[i]); /* Use a dummy item to force the '+' */ if (files[i].isDirectory()) { new TreeItem(item, SWT.NULL); } } } }); /* Set size of the tree and open the shell */ tree.setSize(300, 300); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
请高手赐教~~~
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.SWT;
import java.io.File;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Event;public class Test {
public Test() {
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Lazy Tree");
Tree tree = new Tree(shell, SWT.BORDER);
/* Initialize the roots of the tree */
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) {
TreeItem root = new TreeItem(tree, SWT.NULL);
root.setText(roots[i].toString());
root.setData(roots[i]);
/* Use a dummy item to force the '+' */
new TreeItem(root, SWT.NULL);
}
/* Use SWT.Expand to lazily fill the tree */
tree.addListener(SWT.Expand, new Listener() {
public void handleEvent(Event event) {
/*
* If the item does not contain a
* dummy node, return. A dummy item
* is a single child of the root that
* does not have any application data.
*/
TreeItem root = (TreeItem) event.item;
TreeItem[] items = root.getItems();
if (items.length != 1) return;
if (items[0].getData() != null) return;
items[0].dispose();
/* Create the item children */
File file = (File) root.getData();
File[] files = file.listFiles();
if (files == null) return;
for (int i = 0; i < files.length; i++) {
TreeItem item =new TreeItem(root,SWT.NULL);
item.setText(files[i].getName());
item.setData(files[i]);
/* Use a dummy item to force the '+' */
if (files[i].isDirectory()) {
new TreeItem(item, SWT.NULL);
}
}
}
});
/* Set size of the tree and open the shell */
tree.setSize(300, 300);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
左边tree,右边table
这样大概可以实现
http://sunfruit.blogchina.com/862017.htmlJAVA编写的带有复选框的资源管理器树目录:
http://sunfruit.blogchina.com/985893.html
使用SWT技术TreeView实现。可以看看。给出部分代码。试试看~package TableViewTest;import java.io.File;import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;/**
*
* @author Administrator
* @deprecated 该程序用来模仿window的资源管理器
*
*/
public class TableTreeTest01 extends ApplicationWindow { public TableTreeTest01() {
super(null);
} protected Control createContents(Composite parent) {
TreeViewer tv = new TreeViewer(parent);
tv.setContentProvider(new FileTreeContentProvider());
tv.setInput(new File("C:\\"));
return tv.getTree();
} public static void main(String[] args) {
TableTreeTest01 w = new TableTreeTest01();
w.setBlockOnOpen(true);
w.open();
Display.getCurrent().dispose();
}
private class FileTreeContentProvider implements ITreeContentProvider{ public Object[] getChildren(Object obj) {
// TODO Auto-generated method stub
Object[] kids = ((File)obj).listFiles();
return kids==null?new Object[0]:kids;
} public Object getParent(Object obj) {
// TODO Auto-generated method stub
return ((File)obj).getParent();
} public boolean hasChildren(Object obj) {
// TODO Auto-generated method stub
return getChildren(obj).length > 0;
} public Object[] getElements(Object obj) {
// TODO Auto-generated method stub
return getChildren(obj);
} public void dispose() {
// TODO Auto-generated method stub
} public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// TODO Auto-generated method stub
}
}
private void createActions() {
}
}