Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

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

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.

I tried to implement it using Button(SWT.CHECK) as Table-Editor, but it worked too slow :(

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...