SWT 为表格添加删除修改link_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > SWT 为表格添加删除修改link

SWT 为表格添加删除修改link

 2014/10/29 12:44:58  一纸红颜岂值倾尽天下  程序员俱乐部  我要评论(0)
  • 摘要:窗体程序packagecom.pcm.chni.recipereport.frame;importjava.util.ArrayList;publicclassTableViewerDel{protectedShellshell;privateStringfindLinkKey="FindLink";privateListlistcuCustomers=newArrayList();privateArrayList<Hyperlink>delList=newArrayList<
  • 标签: 表格 SWT
class="java" name="code">窗体程序

package com.pcm.chni.recipereport.frame;

import java.util.ArrayList;

public class TableViewerDel {

	protected Shell shell;
	private String findLinkKey = "FindLink";
	private List listcuCustomers = new ArrayList();
	private ArrayList<Hyperlink> delList = new ArrayList<Hyperlink>();
	private final FormToolkit formToolkit = new FormToolkit(Display
			.getDefault());
	private Table table;
	private TableViewer tableViewer;
	private boolean finashaddlink=false;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TableViewerDel window = new TableViewerDel();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");

		tableViewer = new TableViewer(shell, SWT.BORDER
				| SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(10, 27, 422, 191);
	
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("数据显示");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("删除");
		tableViewer.setContentProvider(new RecipereportTableViewContentProvider());//内容器
		tableViewer.setLabelProvider(new RecipereportTableViewLabelProvider());//标签器
		listcuCustomers.add("zhangsan");
		listcuCustomers.add("lis");
		listcuCustomers.add("wangwu");
		listcuCustomers.add("liuliu");
		listcuCustomers.add("qiqi");
		tableViewer.setInput(listcuCustomers);
		linkListener();
		
		table.addListener(SWT.MeasureItem, new Listener() {//表格监听器 表格画完后设置高度
			public void handleEvent(Event event) {
				event.height = 23;
			}
		});

		Listener delayInit = new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				if(table.getItemHeight() < 23 || listcuCustomers.size() == 0 || finashaddlink){
					return ;
				}
				linkListener();
				finashaddlink = true;
			}
		};
		table.addListener(SWT.MeasureItem, delayInit);

	}

	
	public void linkListener() {

		deleteAllLinks();//删除所有的删除链接
		if (listcuCustomers != null && listcuCustomers.size() > 0) {
			for (int i = 0; i < listcuCustomers.size(); i++) {//获取集合中的数据
				TableItem itemi = table.getItem(i);//表格中一行数据

				final Hyperlink hprlnDeleO = formToolkit.createHyperlink(table,
						"删除", SWT.CENTER);//创建一个删除链接

				formToolkit.paintBordersFor(hprlnDeleO);
				TableEditor editor = new TableEditor(table);//编辑table
				editor.grabHorizontal = editor.grabVertical = true;
				editor.setEditor(hprlnDeleO, itemi, 1);//设置删除链接
				delList.add(hprlnDeleO);//将删除链接添加到delList中

				hprlnDeleO.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent e) {
						 TableItem xxx = table.getItem(new Point(e.x +
								 hprlnDeleO.getBounds().x, e.y +
								 hprlnDeleO.getBounds().y));
						 String hhlfir = (String) xxx.getData();//获取点击这一行的数据

						 listcuCustomers.remove(hhlfir);//点击删除remove listcuCustomers中的数据
						 tableViewer.setInput(listcuCustomers);//重新set数据
						 linkListener();//在每一行中添加删除链接
					}
				});
//				hprlnDeleO.setEnabled(true);//将删除按钮设置为启用
//				hprlnDeleO.setData(findLinkKey, hprlnDeleO);

			}
		}
	}

	protected void deleteAllLinks() {
		for (int i = 0; i < delList.size(); i++) {//循环 删除所有删除链接
			deleteLink(delList.get(i));
		}
		delList.clear();//清空list
	}

	protected void deleteLink(Hyperlink hprlnk_Delete) {
		try {
			if (hprlnk_Delete == null) {
				return;
			}
			hprlnk_Delete.dispose();//摧毁所有的删除链接
//			Hyperlink findlink = (Hyperlink) hprlnk_Delete.getData(findLinkKey);
//			if (findlink != null) {
//				findlink.dispose();
//			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

内容器

package com.pcm.chni.recipereport.provider;

import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class RecipereportTableViewContentProvider implements IStructuredContentProvider{
	@SuppressWarnings("unchecked")
	@Override
	public Object[] getElements(Object inputElement) {
		// TODO Auto-generated method stub
		if(inputElement instanceof List)
            return ((List)inputElement).toArray();
        else
            return new Object[0];
		 
    }
	

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
		// TODO Auto-generated method stub
		
	}
}


标签器

package com.pcm.chni.recipereport.provider;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;

import com.pcm.chni.userManage.model.Customer;

public class RecipereportTableViewLabelProvider implements ITableLabelProvider{

	@Override
	public Image getColumnImage(Object arg0, int arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getColumnText(Object element, int index) {
		
		
		if(element instanceof Customer){
			Customer cu=(Customer)element;
			switch (index) {
			case 0:
				return cu.getCustomerid();
			case 1:
				return cu.getCustomername();
			case 2:
				return cu.getSexid().equals("1")?"男":"女";
			case 3:
				return cu.getBirthday();
			case 4:
				return cu.getMobile();
			case 5:
				return cu.getOrgname();
			case 6:
				return cu.getDocname();
			default:
				return "--";
			}
		}else if(element instanceof String){
			
			switch (index) {
			case 0:
				return (String)element;
			

			default:
				return "--";
			}
		}
		
		return null;
	}

	@Override
	public void addListener(ILabelProviderListener arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isLabelProperty(Object arg0, String arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener arg0) {
		// TODO Auto-generated method stub
		
	}

}



?

发表评论
用户名: 匿名