Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » The image in the table cell could not display in the centre(table tableitem image)
icon5.gif  The image in the table cell could not display in the centre [message #900917] Thu, 09 August 2012 03:29 Go to next message
zhang lei is currently offline zhang lei
Messages: 17
Registered: July 2012
Junior Member
this is my code:
public class DepartList extends Composite {

	Table t;
	Display d;
	public DepartList(Composite parent, int style,Display d) {
		super(parent, style);
		// TODO Auto-generated constructor stub
		this.d = d;
		GridLayout gl = new GridLayout();
		gl.numColumns = 1;
		gl.marginHeight = 0;
		gl.marginWidth = 0;
		this.setLayout(gl);

		t = new Table(this,SWT.FULL_SELECTION|SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_BOTH);
		t.setLayoutData(gd);
		t.setHeaderVisible(true);
		t.setLinesVisible(true);
		t.addListener(SWT.MeasureItem, new Listener(){

			public void handleEvent(Event event) {
				// TODO Auto-generated method stub
				event.height = event.gc.getFontMetrics().getHeight() + 20;
			}
			
		});
		
		
		TableColumn tc1 = new TableColumn(t,SWT.LEFT);
		TableColumn tc2 = new TableColumn(t,SWT.CENTER);
		TableColumn tc3 = new TableColumn(t,SWT.LEFT);
		TableColumn tc4 = new TableColumn(t,SWT.LEFT);
		TableColumn tc5 = new TableColumn(t,SWT.LEFT);
		TableColumn tc6 = new TableColumn(t,SWT.CENTER);
		TableColumn tc7 = new TableColumn(t,SWT.CENTER);
		
		//添加排序的监听器
		tc1.addSelectionListener(new ColumnSorterListener(t,tc1));
		tc2.addSelectionListener(new ColumnSorterListener(t,tc2));
		tc3.addSelectionListener(new ColumnSorterListener(t,tc3));
		tc4.addSelectionListener(new ColumnSorterListener(t,tc4));
		
		
		tc1.setText("序 号");
		tc2.setText("部门类别");
		tc3.setText("部门名称");
		tc4.setText("部门简码");
		tc5.setText("部门位置");
		tc6.setText("递增");
		tc7.setText("递减");
		
		tc1.setWidth(50);
		tc2.setWidth(150);
		tc3.setWidth(150);
		tc4.setWidth(100);
		tc5.setWidth(250);
		tc6.setWidth(50);
		tc7.setWidth(50);
		
		ManipulateDepartment objectManipulateDepartment = new ManipulateDepartment();
		List<MetaDepartment> lmd = objectManipulateDepartment.selectByConstrains(" order by sequence");
		
		if (null!=lmd && lmd.size()>0) {
			for (int j = 0; j < lmd.size(); j++) {
				addTabItem(j+1, lmd.get(j));
			}
		}
	}
	public boolean  addTabItem(int index,MetaDepartment objMetaDepartment){
		boolean bResult = true;
		TableItem objTableItem = new TableItem(t,SWT.NONE);
		int i=0;
		objTableItem.setText(i++,index+"");
		objTableItem.setText(i++, objMetaDepartment.getCategory());
		objTableItem.setText(i++, objMetaDepartment.getDepartmentName());
		objTableItem.setText(i++, objMetaDepartment.getJianMa());
		objTableItem.setText(i++, objMetaDepartment.getLocation());
		objTableItem.setImage(i++,new Image(d,System.getProperty("user.dir")+"\\images\\up.gif"));
		objTableItem.setImage(i++,new Image(d,System.getProperty("user.dir")+"\\images\\down.gif"));
//		objTableItem.seti;
		return bResult;
	}
	public Table geTable(){
		return t;
	}
}

index.php/fa/11087/0/
How to set the image in the center?vertically and horizontally.Thanks.

[Updated on: Thu, 09 August 2012 03:30]

Report message to a moderator

Re: The image in the table cell could not display in the centre [message #900924 is a reply to message #900917] Thu, 09 August 2012 05:54 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5422
Registered: July 2009
Senior Member
You need to use owner draw.

Tom
Am 09.08.12 05:29, schrieb zhang lei:
> this is my code:
>
> public class DepartList extends Composite {
>
> Table t;
> Display d;
> public DepartList(Composite parent, int style,Display d) {
> super(parent, style);
> // TODO Auto-generated constructor stub
> this.d = d;
> GridLayout gl = new GridLayout();
> gl.numColumns = 1;
> gl.marginHeight = 0;
> gl.marginWidth = 0;
> this.setLayout(gl);
>
> t = new Table(this,SWT.FULL_SELECTION|SWT.BORDER);
> GridData gd = new GridData(GridData.FILL_BOTH);
> t.setLayoutData(gd);
> t.setHeaderVisible(true);
> t.setLinesVisible(true);
> t.addListener(SWT.MeasureItem, new Listener(){
>
> public void handleEvent(Event event) {
> // TODO Auto-generated method stub
> event.height = event.gc.getFontMetrics().getHeight() + 20;
> }
>
> });
>
>
> TableColumn tc1 = new TableColumn(t,SWT.LEFT);
> TableColumn tc2 = new TableColumn(t,SWT.CENTER);
> TableColumn tc3 = new TableColumn(t,SWT.LEFT);
> TableColumn tc4 = new TableColumn(t,SWT.LEFT);
> TableColumn tc5 = new TableColumn(t,SWT.LEFT);
> TableColumn tc6 = new TableColumn(t,SWT.CENTER);
> TableColumn tc7 = new TableColumn(t,SWT.CENTER);
>
> //æ·»åŠ æŽ’åºçš„ç›‘å¬å™š
> tc1.addSelectionListener(new ColumnSorterListener(t,tc1));
> tc2.addSelectionListener(new ColumnSorterListener(t,tc2));
> tc3.addSelectionListener(new ColumnSorterListener(t,tc3));
> tc4.addSelectionListener(new ColumnSorterListener(t,tc4));
>
>
> tc1.setText("序 号");
> tc2.setText("郚闚类别");
> tc3.setText("郚闚名称");
> tc4.setText("éƒšé—šç®€ç ");
> tc5.setText("郚闚䜍眮");
> tc6.setText("递增");
> tc7.setText("递减");
>
> tc1.setWidth(50);
> tc2.setWidth(150);
> tc3.setWidth(150);
> tc4.setWidth(100);
> tc5.setWidth(250);
> tc6.setWidth(50);
> tc7.setWidth(50);
>
> ManipulateDepartment objectManipulateDepartment = new ManipulateDepartment();
> List<MetaDepartment> lmd = objectManipulateDepartment.selectByConstrains(" order by sequence");
>
> if (null!=lmd && lmd.size()>0) {
> for (int j = 0; j < lmd.size(); j++) {
> addTabItem(j+1, lmd.get(j));
> }
> }
> }
> public boolean addTabItem(int index,MetaDepartment objMetaDepartment){
> boolean bResult = true;
> TableItem objTableItem = new TableItem(t,SWT.NONE);
> int i=0;
> objTableItem.setText(i++,index+"");
> objTableItem.setText(i++, objMetaDepartment.getCategory());
> objTableItem.setText(i++, objMetaDepartment.getDepartmentName());
> objTableItem.setText(i++, objMetaDepartment.getJianMa());
> objTableItem.setText(i++, objMetaDepartment.getLocation());
> objTableItem.setImage(i++,new Image(d,System.getProperty("user.dir")+"\\images\\up.gif"));
> objTableItem.setImage(i++,new Image(d,System.getProperty("user.dir")+"\\images\\down.gif"));
> // objTableItem.seti;
> return bResult;
> }
> public Table geTable(){
> return t;
> }
> }
>
>
> How to set the image in the center?vertically and horizontally.Thanks.
>
Re: The image in the table cell could not display in the centre [message #900970 is a reply to message #900924] Thu, 09 August 2012 09:08 Go to previous message
zhang lei is currently offline zhang lei
Messages: 17
Registered: July 2012
Junior Member
thank you,I have resolve the problem.the code is
class PaintListener implements Listener{
	private Image upImage;
	private Image downImage;
	private TableColumn tc;
	PaintListener( Image upImage,Image downImage,TableColumn tc){
		this.upImage = upImage;
		this.downImage = downImage;
		this.tc = tc;
	}
    public void handleEvent(Event event) {
        switch (event.type) {
	        case SWT.MeasureItem: {
	          event.height = 40;
	          break;
	        }
	        case SWT.PaintItem: {
	          Rectangle imageRec= upImage.getBounds();
	          if (event.index == 5) {
	        	  event.gc.drawImage(upImage, event.x+tc.getWidth()/2-imageRec.width/2, event.y+event.height/2-imageRec.width/2);
	          }
	          if (event.index == 6) {
	        	  event.gc.drawImage(downImage, event.x+tc.getWidth()/2-imageRec.width/2, event.y+event.height/2-imageRec.width/2);
	          }
	          break;
	        }
        }
        
        
        
      }
	
}

index.php/fa/11089/0/
Previous Topic:Muliselect on swt table items?
Next Topic:Access to pixel data of SWT Image from native libary
Goto Forum:
  


Current Time: Sat Oct 25 20:41:46 GMT 2014

Powered by FUDForum. Page generated in 0.01609 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software