首先我使用SWT中的TreeViewer类画了一棵简单树。
代码如下:
package net.blogjava.lzqdiy.tree;
import java.util.List;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
public class TreeTest
{
class TreeLabelProvider extends LabelProvider
{
public String getText(Object element)
{
ITreeEntry entry = (ITreeEntry) element;
return entry.getName();
}
public Image getImage(Object element)
{
return null;
}
}
class TreeContentProvider implements IStructuredContentProvider,
ITreeContentProvider
{
public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
{
}
public void dispose()
{
}
public Object[] getElements(Object inputElement)
{
if (inputElement instanceof List)
{
List list = (List) inputElement;
return list.toArray();
} else
return new Object[0];
}
public Object[] getChildren(Object parentElement)
{
ITreeEntry entry = (ITreeEntry) parentElement;
List list = entry.getChildren();
if (list == null || list.isEmpty())
{
return new Object[0];
} else
return list.toArray();
}
public Object getParent(Object element)
{
return null;
}
public boolean hasChildren(Object element)
{
ITreeEntry entry = (ITreeEntry) element;
List list = entry.getChildren();
if (list == null || list.isEmpty())
{
return false;
} else
return true;
}
}
private Tree tree;
/** *//**
* Launch the application
*
* @param args
*/
public static void main(String[] args)
{
try
{
TreeTest window = new TreeTest();
window.open();
} catch (Exception e)
{
e.printStackTrace();
}
}
/** *//**
* Open the window
*/
public void open()
{
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(350, 250);
shell.setText("SWT Application");
//
shell.open();
final TreeViewer treeViewer = new TreeViewer(shell, SWT.BORDER);
treeViewer.setLabelProvider(new TreeLabelProvider());
treeViewer.setContentProvider(new TreeContentProvider());
Object inputObj = DataFactory.createTreeData();
treeViewer.setInput(inputObj);
tree = treeViewer.getTree();
tree.setBounds(66, 44, 268, 165);
shell.layout();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
}
}
package net.blogjava.lzqdiy.tree;
import java.util.List;
public interface ITreeEntry
{
String getName();
void setName(String name);
void setChildren(List children);
List getChildren();
}
package net.blogjava.lzqdiy.tree;
import java.util.List;
public class CityEntity implements ITreeEntry
{
public List getChildren()
{
// TODO Auto-generated method stub
return null;
}
public void setChildren(List children)
{
// TODO Auto-generated method stub
}
private long id;
String name;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public CityEntity(String name)
{
super();
this.name = name;
}
}
package net.blogjava.lzqdiy.tree;
import java.util.List;
public class CountryEntity implements ITreeEntry
{
public List getChildren()
{
// TODO Auto-generated method stub
return cities;
}
public void setChildren(List children)
{
// TODO Auto-generated method stub
this.cities=children;
}
private Long id;
private String name;
private List cities;
public List getCities()
{
return cities;
}
public void setCities(List cities)
{
this.cities = cities;
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public CountryEntity(String name)
{
super();
this.name = name;
}
}
package net.blogjava.lzqdiy.tree;
import java.util.ArrayList;
import java.util.List;
public class DataFactory
{
public static Object createTreeData()
{
CityEntity city1 = new CityEntity("北京");
CityEntity city2 = new CityEntity("上海");
CityEntity city3 = new CityEntity("广州");
CountryEntity country = new CountryEntity("中国");
List cities = new ArrayList();
cities.add(city1);
cities.add(city2);
cities.add(city3);
country.setChildren(cities);
List list = new ArrayList();
list.add(country);
return list;
}
}
运行效果:
如果我只需要能画出如上图所示的一棵简单树,使用TreeViewer却要写如此多的代码,实在是得不偿失。这时使我想到了使用Swing中的JTree类。
代码如下:
package net.blogjava.lzqdiy.tree;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeTest2
{
private JTree tree;
private JFrame frame;
/** *//**
* Launch the application
*
* @param args
*/
public static void main(String args[])
{
try
{
TreeTest2 window = new TreeTest2();
window.frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
/** *//**
* Create the application
*/
public TreeTest2()
{
initialize();
}
/** *//**
* Initialize the contents of the frame
*/
private void initialize()
{
frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 400, 275);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultMutableTreeNode country = new DefaultMutableTreeNode("中国");
DefaultMutableTreeNode city1 = new DefaultMutableTreeNode("北京");
DefaultMutableTreeNode city2 = new DefaultMutableTreeNode("上海");
DefaultMutableTreeNode city3 = new DefaultMutableTreeNode("广州");
country.add(city1);
country.add(city2);
country.add(city3);
tree = new JTree(country);
tree.setBounds(113, 76, 222, 120);
frame.getContentPane().add(tree);
}
}
运行效果:
虽然代码变少了,但是感觉用Swing画出的树又没有SWT画出的树漂亮,最终我选择了使用SWT中的Tree类来画一棵简单树。
代码如下:
package net.blogjava.lzqdiy.tree;
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;
public class TreeTest3
{
private Tree tree;
/** *//**
* Launch the application
*
* @param args
*/
public static void main(String[] args)
{
try
{
TreeTest3 window = new TreeTest3();
window.open();
} catch (Exception e)
{
e.printStackTrace();
}
}
/** *//**
* Open the window
*/
public void open()
{
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(350, 250);
shell.setText("SWT Application");
//
shell.open();
tree = new Tree(shell, SWT.BORDER);
tree.setBounds(65, 39, 208, 121);
TreeItem country = new TreeItem(tree, SWT.NONE);
country.setText("中国");
TreeItem city1 = new TreeItem(country, SWT.NULL);
city1.setText("北京");
TreeItem city2 = new TreeItem(country, SWT.NULL);
city2.setText("上海");
TreeItem city3 = new TreeItem(country, SWT.NULL);
city3.setText("广州");
shell.layout();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
}
}
运行效果:
又漂亮,代码量又少,我喜欢,呵呵。