上边介绍了,tableviewer,这里介绍一下treeViewer,总的流程类似。
一、构造一个树形结构的数据出来,这里选择国家、城市、人来构造一个树形结构,代码如下:
1,定义一个接口
packagemodel;
importjava.util.List;
publicinterfaceItree{
publicStringgetName();
publicvoidsetName(Stringname);
publicvoidsetChildren(ListChildren);
publicListgetChildren();
}
2,国家
packagemodel;
importjava.util.ArrayList;
importjava.util.List;
publicclassCountryimplementsItree{
privateLongid;
privateStringname;
privateListchildren=newArrayList();
publicCountry(){
}
publicCountry(Stringname){
this.name=name;
}
publicListgetChildren(){
returnchildren;
}
publicvoidsetChildren(Listchildren){
this.children=children;
}
publicLonggetId(){
returnid;
}
publicvoidsetId(Longid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
3,城市
packagemodel;
importjava.util.ArrayList;
importjava.util.List;
publicclassCityimplementsItree{
privateLongid;
privateStringname;
privateListchildren=newArrayList();
publicCity(){
}
publicCity(Stringname){
this.name=name;
}
publicListgetChildren(){
returnchildren;
}
publicvoidsetChildren(Listchildren){
this.children=children;
}
publicLonggetId(){
returnid;
}
publicvoidsetId(Longid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
4,人
packagemodel;
importjava.util.ArrayList;
importjava.util.List;
publicclassPeopleimplementsItree{
privateLongid;
privateStringname;
publicPeople(){
}
publicPeople(Stringname){
this.name=name;
}
publicListgetChildren(){
returnnull;
}
publicvoidsetChildren(Listchildren){
}
publicLonggetId(){
returnid;
}
publicvoidsetId(Longid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
5,把这些元素组织起来
packagemodel;
importjava.util.ArrayList;
importjava.util.List;
publicclassFactory{
@SuppressWarnings("unchecked")
publicstaticListcreateTree(){
//生成国家
Countrycn=newCountry("中国");
Countryus=newCountry("美国");
Countryjp=newCountry("日本");
//生成城市
Citybeijing=newCity("北京");
Cityshanghai=newCity("上海");
Citynewyork=newCity("纽约");
Cityla=newCity("洛杉矶");
Citytokyo=newCity("东京");
Cityosaka=newCity("大阪");
//北京人
ArrayListlist=newArrayList();
list.add(newPeople("张三"));
list.add(newPeople("李四"));
list.add(newPeople("王五"));
beijing.setChildren(list);
//上海人
list=newArrayList();
list.add(newPeople("翠花"));
list.add(newPeople("小红"));
list.add(newPeople("小崔"));
shanghai.setChildren(list);
//纽约人
list=newArrayList();
list.add(newPeople("tom"));
list.add(newPeople("rose"));
list.add(newPeople("john"));
newyork.setChildren(list);
//洛杉矶人
list=newArrayList();
list.add(newPeople("Sofia"));
list.add(newPeople("sarah"));
list.add(newPeople("Jennifer"));
la.setChildren(list);
//东京人
list=newArrayList();
list.add(newPeople("渡边"));
list.add(newPeople("鬼冢"));
list.add(newPeople("山本"));
tokyo.setChildren(list);
//大阪人
list=newArrayList();
list.add(newPeople("奈奈子"));
list.add(newPeople("菜菜子"));
list.add(newPeople("新垣结衣"));
osaka.setChildren(list);
//关联城市与国家
//中国
ArrayListcitys=newArrayList();
citys.add(beijing);
citys.add(shanghai);
cn.setChildren(citys);
//美国
citys=newArrayList();
citys.add(newyork);
citys.add(la);
us.setChildren(citys);
//日本
citys=newArrayList();
citys.add(tokyo);
citys.add(osaka);
jp.setChildren(citys);
//国家列表
ArrayListcountrys=newArrayList();
countrys.add(cn);
countrys.add(us);
countrys.add(jp);
returncountrys;
}
}
二、定义内容器和标签器
1,内容器
importjava.util.List;
importmodel.Itree;
importorg.eclipse.jface.viewers.IStructuredContentProvider;
importorg.eclipse.jface.viewers.ITreeContentProvider;
importorg.eclipse.jface.viewers.Viewer;
publicclassTreeContentProviderimplementsIStructuredContentProvider,ITreeContentProvider{
publicObject[]getElements(ObjectinputElement){
if(inputElementinstanceofList){
Listinput=(List)inputElement;
returninput.toArray();
}
returnnewObject[0];
}
publicObject[]getChildren(ObjectparentElement){
Itreenode=(Itree)parentElement;
Listlist=node.getChildren();
if(list==null){
returnnewObject[0];
}
returnlist.toArray();
}
publicbooleanhasChildren(Objectelement){
Itreenode=(Itree)element;
Listlist=node.getChildren();
return!(list==null||list.isEmpty());
}
//以下三个函数根据需要填充
publicObjectgetParent(Objectelement){
returnnull;
}
publicvoidinputChanged(Viewerviewer,ObjectoldInput,ObjectnewInput){
}
publicvoiddispose(){
}
}
2,标签器:
importmodel.Itree;
importorg.eclipse.jface.viewers.ILabelProvider;
importorg.eclipse.jface.viewers.LabelProvider;
importorg.eclipse.swt.graphics.Image;
publicclassTreeLabelProviderextendsLabelProviderimplementsILabelProvider{
publicStringgetText(Objectelement){
Itreenode=(Itree)element;
returnnode.getName();
}
publicImagegetImage(Objectelement){
returnnull;
}
}
三、好了,准备工作做好了,把上边的内容利用起来就好了。
importmodel.Factory;
importorg.eclipse.jface.viewers.TreeViewer;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.layout.FillLayout;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
importorg.eclipse.swt.widgets.Tree;
publicclassTestTreeViewer{
privatestaticTreetree;
publicstaticvoidmain(String[]args){
finalDisplaydisplay=Display.getDefault();
finalShellshell=newShell();
shell.setSize(500,375);
shell.setText("SWTApplication");
//
finalTreeViewertreeViewer=newTreeViewer(shell,SWT.BORDER|SWT.H_SCROLL);
tree=treeViewer.getTree();
tree.setBounds(83,75,264,185);
treeViewer.setLabelProvider(newTreeLabelProvider());
treeViewer.setContentProvider(newTreeContentProvider());
treeViewer.setInput(Factory.createTree());
shell.open();
shell.setLayout(newFillLayout());
shell.layout();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
}
看看是不是已经实现了treeViewer
四、上边是简单的treeViewer,如果我们需要带checkbox的treeViewer,简单,只需要更改
finalTreeViewertreeViewer=newTreeViewer(shell,SWT.BORDER|SWT.H_SCROLL);
把TreeViewer换成CheckboxTreeViewer。这就是mvc的好处了。
但现实中我们多用ContainerCheckedTreeViewer代替CheckboxTreeViewer,因为这个提供了更多的功能。
CheckboxTreeViewer是TreeViewer的子类, ContainerCheckedTreeViewer是CheckboxTreeViewer的子类,所以可以随便替换。
替换后:
finalContainerCheckedTreeViewertreeViewer=newContainerCheckedTreeViewer(shell,SWT.BORDER|SWT.H_SCROLL);
五、treeviewer和tableviewer都介绍过了,考虑一下把两个结合起来是不是我们经常需要的情况。尝试一下吧。
六、souce下载:
http://www.blogjava.net/Files/dreamstone/jface-6.rar
分享到:
相关推荐
swt/Jface用到的最基本的jar包
SWT/JFACE API,是关于SWT/JFACE开发所用的API
SWT/Jface API 3.4 SWT/Jface API 3.4 SWT/Jface API 3.4 SWT/Jface API 3.4
Eclipse SWT/JFace SWT JFace 实例 Eclipse SWT/JFace 书中 全部实例
Eclipse SWT/JFace 核心应用光盘源码下载,解压后即可使用
Eclipse SWT/JFace核心应用
本文档为swt/jface的api中文帮助文档。
SWT/Jface 开发入门指南 介绍以下方面的内容: ? 设定 swt 以及 JFace 的开发环境 ? swt 的一些简单部件(widget)介绍 ? JFace 的一些入门性介绍 ? swt 和 JFace 的事件模式 ? 通过一些简单的例子说明如何利用 swt ...
SWT/JFace开发实例
Eclipse SWT/JFace 核心应用 带书签 Eclipse SWT/JFace
eclipse swt/jface核心应用 这本书的源代码
eclipse swt/jface核心应用源代码 带视频,找了好久,希望可以帮到大家,跟书匹配
SWT/JFace 3.5 版本的 API,HTML格式,方便开发人员查阅。
swt/jface in action 中英文 pdf格式 + 源码下载,包含书中的所有源码,有需要的朋友可以下载,为正在学习swt、jface的朋友提供一些好资源。
基于 Java 桌面程序开发的图形库...本书主要讲解了如何使用 SWT 和 JFace 进行应用程序的开发,通过本书系统而全面的 SWT/JFace 知识学习,将帮助读者快 速开发出完美、实用的 GUI 程序,轻松完成繁琐的界面、菜单编程.
王占全,苏玲 著; ISBN:7-121-05152-4 学习 SWT/JFace 的好书!
Eclipse SWT/JFace 开发实战精解+完美书签+光盘源码完整版. pdf带书签 ,代码完整的光盘,赚点积分。
《Eclipse SWT/Jface核心应用》...第2篇介绍了SWT基本控件的使用,以及事件处理、布局等SWT基本知识的应用。第3篇介绍了关于SWT的高级应用。第4篇介绍了JFace框架的知识及其应用。第5篇介绍了最新的RCP应用程序的开发。
NULL 博文链接:https://niub.iteye.com/blog/1806580