如何将图像与表格单元格的中心对齐(SWT表) [英] How to align image to center of table cell (SWT Table)
问题描述
我开发了Eclipse RCP应用程序并遇到了表
的问题。我们在数据库中有一些布尔格式的数据,用户希望使用复选框
来查看该字段。
I develop Eclipse RCP application and got a problem with a Table
. We have some data in database in boolean format and users wants to see that field using checkbox
.
我试过使用 Button(SWT.CHECK)
实现它作为表编辑器,但它工作得太慢了:(
I tried to implement it using Button(SWT.CHECK)
as Table-Editor, but it worked too slow :(
我尝试使用2张图片 - 经过检查和取消选中的复选框,但是我无法将它们对齐到中心,它们会自动对齐。
I tried to use 2 images - checked and unchecked check-boxes, it works, but I can't align them to center, they are aligned to left automatically.
我甚至找到了如何捕获 SWT.MeasureItem
和 SWT.PaintItem
事件并手动处理它们更改 event.x
字段,但我遇到了问题 - 我目前无法获得什么列测量或绘画,因为事件不会向我提供该信息。
I even found how to catch SWT.MeasureItem
and SWT.PaintItem
events and process them manually by change event.x
field, but I got a problem - I can't get what column measuring or painting at the moment, because Event doesn't provides me that information.
这是通过重绘时修改事件数据将图像对齐到中心的唯一方法,还是可以通过其他方式通过复选框表示布尔数据?我不知道现在需要编辑它们,所以只读模式就足够了。
Is it the only way to align images to center by modify event data on redrawing, or may be there is other ways to represent boolean data via check-boxes? I don't need to edit them now, so read-only mode should be enough.
推荐答案
您可以将 PaintListener
添加到您的表中以及何时绘制所选列(在我的情况下为第5列),检查行的大小和自己对齐图像..
You may add PaintListener
to your table and when it will paint selected column (column 5 in my case), check the size of row and align the image by yourself..
testTable.addListener(SWT.PaintItem, new Listener() {
@Override
public void handleEvent(Event event) {
// Am I on collumn I need..?
if(event.index == 5) {
Image tmpImage = IMAGE_TEST_PASS;
int tmpWidth = 0;
int tmpHeight = 0;
int tmpX = 0;
int tmpY = 0;
tmpWidth = testTable.getColumn(event.index).getWidth();
tmpHeight = ((TableItem)event.item).getBounds().height;
tmpX = tmpImage.getBounds().width;
tmpX = (tmpWidth / 2 - tmpX / 2);
tmpY = tmpImage.getBounds().height;
tmpY = (tmpHeight / 2 - tmpY / 2);
if(tmpX <= 0) tmpX = event.x;
else tmpX += event.x;
if(tmpY <= 0) tmpY = event.y;
else tmpY += event.y;
event.gc.drawImage(tmpImage, tmpX, tmpY);
}
}
});
这篇关于如何将图像与表格单元格的中心对齐(SWT表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!