重点内容:
tableViewer
内容:
创建tableviewer
三种构造方法:
1。TableViewer(Composite parent)
2.TableViewer(Composite parent,int style);
3.TableViewer(Table table)
通过table.getTable()可以获得Table对象
TableViewer将组织和显示的数据交给了两个接口,
IcontentProvider : 负责将SETinput方法传入的对象转成表格所需要的对象。
|-- IStructuredContentProvider
IBaseLabelProvider
|--ITableLabelProvider:负责处理表如如保显示数.
一般步骤:
// 创建TableView
table = new TableViewer(parent, SWT.FULL_SELECTION);
// 创建表头
for (int i = 0; i < COLUMN_NAME.length; i++) {
new TableColumn(table.getTable(), SWT.LEFT).setText(COLUMN_NAME[i]);
table.getTable().getColumn(i).pack();
}
// 设置表头与表格线可见
table.getTable().setHeaderVisible(true);
table.getTable().setLinesVisible(true);
// 设置视图
table.setContentProvider(new MyContentProvider());
table.setLabelProvider(new MylabelProvider());
//
table.setInput(persons);
加菜单时易错地方:
private Menu md; //要定义全局变量
private TableViewer table; //目标表格。action初始化要用。
private List persons; //表格结构实数据.
//PersonEO person = (PersonEO) element;实体结构类
//下面生成菜单
private void createContextMenu(Composite parent) {
MenuManager menuManager = new MenuManager();
menuManager.add(new addAction(table));
menuManager.add(new DelAction(table));
md = menuManager.createContextMenu(parent);
table.getTable().setMenu(md);
}
protected Control createContents(Composite parent) {
// 创建TableView
table = new TableViewer(parent, SWT.FULL_SELECTION);
// 创建表头
for (int i = 0; i < COLUMN_NAME.length; i++) {
new TableColumn(table.getTable(), SWT.LEFT).setText(COLUMN_NAME[i]);
table.getTable().getColumn(i).pack();
}
// 设置表头与表格线可见
table.getTable().setHeaderVisible(true);
table.getTable().setLinesVisible(true);
// 设置视图
table.setContentProvider(new MyContentProvider());
table.setLabelProvider(new MylabelProvider());
//
table.setInput(persons);
table.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
StructuredSelection selection = (StructuredSelection) event
.getSelection();
PersonEO personEO = (PersonEO) selection.getFirstElement();
MessageBox mb = new MessageBox(getShell(), SWT.YES);
mb.setMessage(personEO.getName().toString());
mb.open();
}
});
createContextMenu(parent); //这句很重要,
return parent;
}
Action操作注意:
要把上面主程中的TABLE传入action操作:
public class addAction extends Action {
TableViewer table; //接收目标table
public addAction(TableViewer table) {
this.table = table;
setText("新建");
}
@Override
public void run() {
PersonEO personEO = new PersonEO();
personEO.setId(table.getTable().getItemCount() + 1);
personEO.setName("keim");
personEO.setGender("女");
personEO.setColor("红色");
table.add(personEO);
}
}
主程式代码:
package com.cmo.mes.table;
import java.awt.Dialog;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
public class TableWindow extends ApplicationWindow {
private Menu md;
private TableViewer table;
private List persons;
public static final int ID = 0;
public static final int NAME = 1;
public static final int GENDER = 2;
public static final int COLOR = 3;
public static final String[] COLUMN_NAME = { "ID号", "姓名", "性别", "COLOR" };
public TableWindow() {
super(null);
initPersons();
// TODO Auto-generated constructor stub
}
private void initPersons() {
persons = new ArrayList();
persons.add(new PersonEO(1, "ZQM", "男", "灰色"));
persons.add(new PersonEO(2, "cxh", "男", "灰色"));
}
@Override
protected void configureShell(Shell shell) {
// TODO Auto-generated method stub
super.configureShell(shell);
shell.setSize(300, 200);
shell.setText("table view 程序示列");
}
protected Control createContents(Composite parent) {
// 创建TableView
table = new TableViewer(parent, SWT.FULL_SELECTION);
// 创建表头
for (int i = 0; i < COLUMN_NAME.length; i++) {
new TableColumn(table.getTable(), SWT.LEFT).setText(COLUMN_NAME[i]);
table.getTable().getColumn(i).pack();
}
// 设置表头与表格线可见
table.getTable().setHeaderVisible(true);
table.getTable().setLinesVisible(true);
// 设置视图
table.setContentProvider(new MyContentProvider());
table.setLabelProvider(new MylabelProvider());
//
table.setInput(persons);
table.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
StructuredSelection selection = (StructuredSelection) event
.getSelection();
PersonEO personEO = (PersonEO) selection.getFirstElement();
MessageBox mb = new MessageBox(getShell(), SWT.YES);
mb.setMessage(personEO.getName().toString());
mb.open();
}
});
createContextMenu(parent);
return parent;
}
private void createContextMenu(Composite parent) {
MenuManager menuManager = new MenuManager();
menuManager.add(new addAction(table));
menuManager.add(new DelAction(table));
md = menuManager.createContextMenu(parent);
table.getTable().setMenu(md);
}
public static void main(String[] args) {
TableWindow test = new TableWindow();
test.setBlockOnOpen(true);
test.open();
Display.getCurrent().dispose();
}
}
自定义类MylabelProvider :
package com.cmo.mes.table;
import java.util.List;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import com.cmo.mes.table.TableWindow;
import com.sun.org.apache.bcel.internal.generic.RETURN;
public class MylabelProvider implements ITableLabelProvider {
@Override
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
PersonEO person = (PersonEO) element;
if (columnIndex == TableWindow.ID) {
return person.getId() + "";
} else if (columnIndex == TableWindow.NAME) {
return person.getName();
} else if (columnIndex == TableWindow.COLOR) {
return person.getColor();
} else if (columnIndex == TableWindow.GENDER) {
return person.getGender();
} else {
return "";
}
}
@Override
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}
@Override
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
}
自定义类MyContentProvider :
package com.cmo.mes.table;
import java.util.List;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class MyContentProvider implements IStructuredContentProvider {
@Override
// 将初始化数据的入口对象转换成表格使用的数组对象
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
return ((List) inputElement).toArray();
}
@Override
// 释放该对象
public void dispose() {
// TODO Auto-generated method stub
}
@Override
// 当表格中有数据改变时调用 该方法。
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
}
add action 代码:
package com.cmo.mes.table;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Table;
public class addAction extends Action {
TableViewer table;
public addAction(TableViewer table) {
this.table = table;
setText("新建");
}
@Override
public void run() {
PersonEO personEO = new PersonEO();
personEO.setId(table.getTable().getItemCount() + 1);
personEO.setName("keim");
personEO.setGender("女");
personEO.setColor("红色");
table.add(personEO);
}
}
deleteaction 代码:
package com.cmo.mes.table;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.widgets.Event;
public class DelAction extends Action {
TableViewer table;
public DelAction(TableViewer table) {
setText("删除");
this.table = table;
}
@Override
public void run() {
StructuredSelection selctioSelection = (StructuredSelection) table
.getSelection();
PersonEO personEO = (PersonEO) selctioSelection.getFirstElement();
table.remove(personEO);
}
}
实体类:
package com.cmo.mes.table;
public class PersonEO {
private int id;
private String name;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private String color;
public PersonEO(int id, String name, String gender, String color) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.color = color;
}
public PersonEO() {
super();
}
}