⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snippet230.java

📁 SWT的Table 可以实现在Table中插入任何继承自Composite的控件
💻 JAVA
字号:
/* 
 * Table example snippet: Images on the right hand side of a TableItem
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import test.JCPControl;

public class Snippet230 {
	static int flag = 0;
	static int width,height;
	
public static void main(String [] args) {
	Display display = new Display();
	final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
	Shell shell = new Shell(display);
	shell.setText("Images on the right side of the TableItem");
	shell.setLayout(new FillLayout ());
	final Image img = new Image(display,"Search.gif");
	final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
	table.setHeaderVisible(true);
	table.setLinesVisible(true);			
	int columnCount = 3;
	for (int i=0; i<columnCount; i++) {
		TableColumn column = new TableColumn(table, SWT.NONE);
		column.setText("Column " + i);	
		if(i==0)column.setWidth(160);
	}
	int itemCount = 3;
	String str = "F:\\Chem data\\2Dcml";
	String []molFiles = {"1.mol","dd.mol","asd.mol"};
	JCPControl btn;
	for(int i = 0; i < itemCount; i++) {
		TableItem item = new TableItem(table, SWT.NONE);
		item.setText(new String[] {"item "+i+" a", "item "+i+" b", "item "+i+" c"});
		btn = new JCPControl(table , SWT.EMBEDDED);
        btn.setFilename(str+"\\"+molFiles[i]);
        btn.loadComponent();
		item.setData(btn);
	}
	
	/*
	 * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
	 * Therefore, it is critical for performance that these methods be
	 * as efficient as possible.
	 */
	Listener paintListener = new Listener() {
		public void handleEvent(Event event) {		
			switch(event.type) {
				case SWT.MeasureItem: {
					if(event.index == 0)
					{
						event.width = table.getColumn(0).getWidth();
						event.height = 160;//event.width;
						System.out.println("measure" + event.width + ", " + event.height);
					}
					else
					{
						event.width =table.getColumn(event.index).getWidth();
					//	event.height = table.getColumn(0).getWidth();
						event.height = 160;
						//System.out.println("measureSecond" + event.width + ", " + event.height);
					}
					break;
				}
				case SWT.PaintItem: {
					System.out.println("index: " + event.index);
					if(event.index == 0)
						
					{    
						//System.out.println("paint item" + event.width + ", " + event.height);
						JCPControl btn = (JCPControl)event.item.getData();
						//System.out.println(btn.toString());
//						Button btn = (Button)event.item.getData();
//						Canvas btn = (Canvas)event.item.getData();
						btn.setLocation(0, event.y);
						System.out.println(btn.getLocation());
						
						btn.setSize(table.getColumn(0).getWidth(),event.height-1);
						btn.setDimension(table.getColumn(0).getWidth(), event.height-1);
						btn.getDrawingPanel().repaint();
		
						System.out.println(table.getColumn(0).getWidth()+"       "+event.height);
					}
					break;
				}
			}
		}
	};		
	table.addListener(SWT.MeasureItem, paintListener);
	table.addListener(SWT.PaintItem, paintListener);	

	for(int i = 0; i < columnCount; i++) {
		table.getColumn(i).pack();
	}	
	shell.setSize(500, 200);
	shell.open();
	while(!shell.isDisposed ()) {
		if(!display.readAndDispatch()) display.sleep();
	}
	if(image != null) image.dispose();
	display.dispose();
}

public void setSize(int width,int height){
	this.width = width;
	this.height = height;
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -