简体   繁体   中英

JFace TableViewer draw a rectangle on cell like Excel

I am trying to achieve a Excel like functionality in a TableViewer to draw a rectangle in a highlighted cell. Below is the code snippet doing this work, but it is not working completely as only left and top edges are getting drawn and I am not able to understand why the bottom and right edges are getting missed!

Code snippet :

private void markFocusedCell(Event event, ViewerCell cell) {
        GC gc = event.gc;

        event.gc.setAlpha(200);
        event.gc.setForeground(event.display.getSystemColor(SWT.COLOR_RED));

        Rectangle rect = cell.getBounds();

        gc.drawRectangle(rect.x, rect.y, rect.width, rect.height);
        event.gc.setForeground(event.display.getSystemColor(SWT.COLOR_BLACK));

        event.detail &= ~SWT.SELECTED;
    }

Output

在此处输入图片说明

您只需要向右上方/下方绘制1像素的底线和底线:

gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1);

By the looks of it, the drawn rectangle is as big as the cell, and therefore, it does not fit inside it.

I would try drawing in the following way:

gc.drawRectangle(rect.x, rect.y, rect.width-1, rect.height-1);

如果您想要更多类似Excel的功能,为什么不使用NatTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM