JFace TableViewer

TableViewer初入

本例会实现些效果 

JFace TableViewer_第1张图片 
右键菜单 

JFace TableViewer_第2张图片 
双击一行 

JFace TableViewer_第3张图片 


首先新建PeopleEntity,此类为假想数据表entity 
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://favor.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20com.mypractice%3B%0A%0Aimport%20java.util.Date%3B%0A%0Apublic%20class%20PeopleEntity%20%7B%0A%0A%09private%20Long%20id%3B%0A%0A%09private%20String%20name%3B%0A%0A%09private%20boolean%20sex%3B%0A%0A%09private%20int%20age%3B%0A%0A%09private%20Date%20createDate%3B%0A%0A%09public%20int%20getAge()%20%7B%0A%09%09return%20age%3B%0A%09%7D%0A%0A%09public%20void%20setAge(int%20age)%20%7B%0A%09%09this.age%20%3D%20age%3B%0A%09%7D%0A%0A%09public%20Date%20getCreateDate()%20%7B%0A%09%09return%20createDate%3B%0A%09%7D%0A%0A%09public%20void%20setCreateDate(Date%20createDate)%20%7B%0A%09%09this.createDate%20%3D%20createDate%3B%0A%09%7D%0A%0A%09public%20Long%20getId()%20%7B%0A%09%09return%20id%3B%0A%09%7D%0A%0A%09public%20void%20setId(Long%20id)%20%7B%0A%09%09this.id%20%3D%20id%3B%0A%09%7D%0A%0A%09public%20String%20getName()%20%7B%0A%09%09return%20name%3B%0A%09%7D%0A%0A%09public%20void%20setName(String%20name)%20%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%7D%0A%0A%09public%20boolean%20isSex()%20%7B%0A%09%09return%20sex%3B%0A%09%7D%0A%0A%09public%20void%20setSex(boolean%20sex)%20%7B%0A%09%09this.sex%20%3D%20sex%3B%0A%09%7D%0A%7D%0A"></embed>
  1. package com.mypractice;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class PeopleEntity {  
  6.   
  7.     private Long id;  
  8.   
  9.     private String name;  
  10.   
  11.     private boolean sex;  
  12.   
  13.     private int age;  
  14.   
  15.     private Date createDate;  
  16.   
  17.     public int getAge() {  
  18.         return age;  
  19.     }  
  20.   
  21.     public void setAge(int age) {  
  22.         this.age = age;  
  23.     }  
  24.   
  25.     public Date getCreateDate() {  
  26.         return createDate;  
  27.     }  
  28.   
  29.     public void setCreateDate(Date createDate) {  
  30.         this.createDate = createDate;  
  31.     }  
  32.   
  33.     public Long getId() {  
  34.         return id;  
  35.     }  
  36.   
  37.     public void setId(Long id) {  
  38.         this.id = id;  
  39.     }  
  40.   
  41.     public String getName() {  
  42.         return name;  
  43.     }  
  44.   
  45.     public void setName(String name) {  
  46.         this.name = name;  
  47.     }  
  48.   
  49.     public boolean isSex() {  
  50.         return sex;  
  51.     }  
  52.   
  53.     public void setSex(boolean sex) {  
  54.         this.sex = sex;  
  55.     }  
  56. }  

然后创建PeopleFactory,用来生成PeopleEntity 
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://favor.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20com.mypractice%3B%0A%0Aimport%20java.util.ArrayList%3B%0Aimport%20java.util.Date%3B%0Aimport%20java.util.List%3B%0A%0Apublic%20class%20PeopleFactory%20%7B%0A%0A%09public%20static%20List%3CPeopleEntity%3E%20getPeoples()%20%7B%0A%09%09List%3CPeopleEntity%3E%20list%20%3D%20new%20ArrayList%3CPeopleEntity%3E()%3B%0A%09%09%7B%0A%09%09%09PeopleEntity%20o%20%3D%20new%20PeopleEntity()%3B%0A%09%09%09o.setId(new%20Long(1))%3B%0A%09%09%09o.setName(%22%E8%B5%B5%E4%BA%91%22)%3B%0A%09%09%09o.setSex(true)%3B%0A%09%09%09o.setAge(28)%3B%0A%09%09%09o.setCreateDate(new%20Date())%3B%0A%09%09%09list.add(o)%3B%0A%09%09%7D%0A%09%09%0A%09%09%7B%0A%09%09%09PeopleEntity%20o%20%3D%20new%20PeopleEntity()%3B%0A%09%09%09o.setId(new%20Long(2))%3B%0A%09%09%09o.setName(%22%E5%85%B3%E7%BE%BD%22)%3B%0A%09%09%09o.setSex(true)%3B%0A%09%09%09o.setAge(20)%3B%0A%09%09%09o.setCreateDate(new%20Date())%3B%0A%09%09%09list.add(o)%3B%0A%09%09%7D%0A%09%09%7B%0A%09%09%09PeopleEntity%20o%20%3D%20new%20PeopleEntity()%3B%0A%09%09%09o.setId(new%20Long(3))%3B%0A%09%09%09o.setName(%22%E6%9B%B9%E6%93%8D%22)%3B%0A%09%09%09o.setSex(true)%3B%0A%09%09%09o.setAge(23)%3B%0A%09%09%09o.setCreateDate(new%20Date())%3B%0A%09%09%09list.add(o)%3B%0A%09%09%7D%0A%09%09%7B%0A%09%09%09PeopleEntity%20o%20%3D%20new%20PeopleEntity()%3B%0A%09%09%09o.setId(new%20Long(4))%3B%0A%09%09%09o.setName(%22%E5%BC%A0%E9%A3%9E%22)%3B%0A%09%09%09o.setSex(false)%3B%0A%09%09%09o.setAge(30)%3B%0A%09%09%09o.setCreateDate(new%20Date())%3B%0A%09%09%09list.add(o)%3B%0A%09%09%7D%0A%09%09return%20list%3B%0A%09%7D%0A%7D%0A"></embed>
  1. package com.mypractice;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Date;  
  5. import java.util.List;  
  6.   
  7. public class PeopleFactory {  
  8.   
  9.     public static List<PeopleEntity> getPeoples() {  
  10.         List<PeopleEntity> list = new ArrayList<PeopleEntity>();  
  11.         {  
  12.             PeopleEntity o = new PeopleEntity();  
  13.             o.setId(new Long(1));  
  14.             o.setName("赵云");  
  15.             o.setSex(true);  
  16.             o.setAge(28);  
  17.             o.setCreateDate(new Date());  
  18.             list.add(o);  
  19.         }  
  20.           
  21.         {  
  22.             PeopleEntity o = new PeopleEntity();  
  23.             o.setId(new Long(2));  
  24.             o.setName("关羽");  
  25.             o.setSex(true);  
  26.             o.setAge(20);  
  27.             o.setCreateDate(new Date());  
  28.             list.add(o);  
  29.         }  
  30.         {  
  31.             PeopleEntity o = new PeopleEntity();  
  32.             o.setId(new Long(3));  
  33.             o.setName("曹操");  
  34.             o.setSex(true);  
  35.             o.setAge(23);  
  36.             o.setCreateDate(new Date());  
  37.             list.add(o);  
  38.         }  
  39.         {  
  40.             PeopleEntity o = new PeopleEntity();  
  41.             o.setId(new Long(4));  
  42.             o.setName("张飞");  
  43.             o.setSex(false);  
  44.             o.setAge(30);  
  45.             o.setCreateDate(new Date());  
  46.             list.add(o);  
  47.         }  
  48.         return list;  
  49.     }  
  50. }  

创建OpenAction,生成右键菜单"打开" 
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://favor.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20com.mypractice%3B%0A%0Aimport%20org.eclipse.jface.action.Action%3B%0Aimport%20org.eclipse.jface.dialogs.MessageDialog%3B%0Aimport%20org.eclipse.jface.viewers.IStructuredSelection%3B%0Aimport%20org.eclipse.jface.viewers.TableViewer%3B%0A%0Apublic%20class%20OpenAction%20extends%20Action%20%7B%0A%0A%09private%20TableViewer%20tv%3B%0A%09%0A%09public%20OpenAction(TableViewer%20tv)%20%7B%0A%09%09this.tv%20%3D%20tv%3B%0A%09%09setText(%22%E6%89%93%E5%BC%80%22)%3B%0A%09%7D%0A%09%0A%09public%20void%20run()%20%7B%0A%09%09IStructuredSelection%20selection%20%3D%20(IStructuredSelection)%20tv.getSelection()%3B%0A%09%09PeopleEntity%20o%20%3D%20(PeopleEntity)%20selection.getFirstElement()%3B%0A%09%09if(o%20%3D%3D%20null)%0A%09%09%09MessageDialog.openInformation(null%2C%20null%2C%20%22%E8%AF%B7%E5%85%88%E9%80%89%E6%8B%A9%E8%AE%B0%E5%BD%95%22)%3B%0A%09%09else%0A%09%09%09MessageDialog.openInformation(null%2C%20null%2C%20o.getName())%3B%0A%09%7D%0A%7D%0A"></embed>
  1. package com.mypractice;  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.dialogs.MessageDialog;  
  5. import org.eclipse.jface.viewers.IStructuredSelection;  
  6. import org.eclipse.jface.viewers.TableViewer;  
  7.   
  8. public class OpenAction extends Action {  
  9.   
  10.     private TableViewer tv;  
  11.       
  12.     public OpenAction(TableViewer tv) {  
  13.         this.tv = tv;  
  14.         setText("打开");  
  15.     }  
  16.       
  17.     public void run() {  
  18.         IStructuredSelection selection = (IStructuredSelection) tv.getSelection();  
  19.         PeopleEntity o = (PeopleEntity) selection.getFirstElement();  
  20.         if(o == <span c
分享到:
评论

你可能感兴趣的:(eclipse,Flash,Go)