花开有时

花开有时,花落有时,来有时,去有时。不撕扯,只关切;不纪念,只牵挂;不感动,只明白;不寻找,只记得。
随笔 - 24, 文章 - 0, 评论 - 54, 引用 - 0
数据加载中……

如何自定义SWT Table的每一个Cell的height and width

demonstration code:
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,200,250);
final Table table = new Table(shell, SWT.NONE);
table.setBounds(10,10,150,200);
table.setLinesVisible(true);
for (int i = 0; i < 5; i++) {
new TableItem(table, SWT.NONE).setText("item " + i);
}
table.addListener(SWT.MeasureItem, new Listener() { 
public void handleEvent(Event event) { 
int clientWidth = table.getClientArea().width;
event.height = event.gc.getFontMetrics().getHeight() * 2; 
event.width = clientWidth * 2; 
 } 
});  
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();

说明:黑体部分是关键,主要是要为table添加一个监听器并要用到SWT.MeasureItem属性,如果是在TableView下则需要调用TableView#getTable()方法获得Table再添加监听器。
还需要注意的是:
1)这个特性可能只在Eclipse3.2以上的版本中才有。
2)不能对单行(Item)进行height的设置。
3)如果设置了width,则column的width以它为准。
参考文章:[[1]]http://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm#_tb10D

posted on 2008-02-03 16:26 花开有时 阅读(2384) 评论(1)  编辑  收藏 所属分类: Eclipse

评论

# re: 如何自定义SWT Table的每一个Cell的height and width  回复  更多评论   

非常 不错呀。收藏
2008-02-17 12:34 | 91cn99

只有注册用户登录后才能发表评论。


网站导航: