How to align image to center of table cell (SWT Table)

后端 未结 4 1923
既然无缘
既然无缘 2020-12-11 05:22

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

4条回答
  •  独厮守ぢ
    2020-12-11 05:47

    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);
            }
        }
    });
    

提交回复
热议问题