2009-12-23 83 views
3

一旦我開始在Eclipse表格單元格中繪製自己的圖像,突出顯示錶格單元格導致奇怪的高亮顏色。見自己:Eclipse:表列圖像是否透明?

alt text

雖然透明度實際上是像保存在第一列中,突出藍色並不像它應該爲藍色。這是我的錯還是Eclipse的錯?

下面的代碼片段:

public class TransparentOrNot { 

    public static void main(String[] args) { 
    Display firstDisplay = new Display(); 
    Shell firstShell = new Shell(firstDisplay); 
    firstShell.setText("Transparent-or-not!"); 
    firstShell.setSize(300, 200); 
    firstShell.setLayout(new FillLayout()); 

    TableViewer viewer = new TableViewer(firstShell, SWT.MULTI); 
    viewer.getTable().setLinesVisible(true); 
    viewer.getTable().setHeaderVisible(true); 

    TableViewerColumn tableViewerColumn = new TableViewerColumn(viewer, SWT.CENTER); 
    tableViewerColumn.getColumn().setText("First"); 
    tableViewerColumn.getColumn().setWidth(150); 
    tableViewerColumn.setLabelProvider(new ColumnLabelProvider() { 

     @Override 
     public Image getImage(Object element) { 
     return ImageDescriptor.createFromFile(TransparentOrNot.class, "/red.png").createImage(); 
     } 

     @Override 
     public String getText(Object element) { 
     return null; 
     } 
    }); 
    tableViewerColumn = new TableViewerColumn(viewer, SWT.CENTER); 
    tableViewerColumn.getColumn().setText("Second"); 
    tableViewerColumn.getColumn().setWidth(150); 
    tableViewerColumn.setLabelProvider(new CenterImageLabelProvider()); 
    viewer.setContentProvider(ArrayContentProvider.getInstance()); 

    viewer.setInput(new String[][]{{"a", "b"}, {"c", "d"}}); 

    firstShell.open(); 
    while (!firstShell.isDisposed()) { 
     if (!firstDisplay.readAndDispatch()) { 
     firstDisplay.sleep(); 
     } 
    } 
    firstDisplay.dispose(); 
    } 

    static class CenterImageLabelProvider extends OwnerDrawLabelProvider { 

    @Override 
    protected void measure(Event event, Object element) { 
     // no-op 
    } 

    @Override 
    protected void paint(Event event, Object element) { 

     Image image = ImageDescriptor.createFromFile(TransparentOrNot.class, "/green.png").createImage(); 

     Widget item = event.item; 
     Rectangle bounds = ((TableItem) item).getBounds(event.index); 

     Rectangle imgBounds = image.getBounds(); 
     bounds.width /= 2; 
     bounds.width -= imgBounds.width/2; 
     bounds.height /= 2; 
     bounds.height -= imgBounds.height/2; 

     int x = bounds.width > 0 ? bounds.x + bounds.width : bounds.x; 
     int y = bounds.height > 0 ? bounds.y + bounds.height : bounds.y; 

     event.gc.drawImage(image, x, y); 
    } 
    } 
} 
+1

有沒有出去檢查出http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html的線索? – VonC

回答

2

重寫擦除()和調用super.erase()的幫助。

+0

您是否設法獲得默認系統「選擇」風格?在Win 7上面對同樣的問題,我想顯示的不是一個自定義的背景,而是一個窗口 – Mirco