带列的SWT树:在Linux/Mac上删除行的交替背景色




我正在用列为树的某些列着色(看起来像一个表)。在Windows系统上,它看起来不错,但在Linux/Mac上则不然,因为它们使用交替的背景色(白色、蓝色、白色、蓝色…)。
我想删除树的交替背景色。如果可能的话,有什么想法吗?我试图在这些系统上禁止tree.setLinesVisible(true),但它也不会画垂直线,这是我需要的。

我认为您可以使用所有者绘制来完成此操作。使用此代码段作为起点,并使用以下内容交换EraseItem事件处理程序:

public void handleEvent( Event event ) {
   event.detail &= ~SWT.HOT;
   GC gc = event.gc;
   Rectangle area = table.getClientArea();
   /*
    * If you wish to paint the selection beyond the end of
    * last column, you must change the clipping region.
    */
   int columnCount = table.getColumnCount();
   if ( event.index == columnCount - 1 || columnCount == 0 ) {
      int width = area.x + area.width - event.x;
      if ( width > 0 ) {
         Region region = new Region();
         gc.getClipping(region);
         region.add(event.x, event.y, width, event.height);
         gc.setClipping(region);
         region.dispose();
      }
   }
   gc.setAdvanced(true);
   if ( gc.getAdvanced() ) gc.setAlpha(127);
   Rectangle rect = event.getBounds();
   Color background = gc.getBackground();
   gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
   gc.fillRectangle(0, rect.y, 500, rect.height);
   // restore colors for subsequent drawing
   gc.setBackground(background);
}

应该在GTK(SWT使用的linux窗口管理器)和Trees上工作,但还没有尝试过。

由于所有者抽取是昂贵的,我强烈建议为GTK添加这样一个事件处理程序:

if(SWT.getPlatform().equals("gtk")){ ... }

但SWT的全部意义在于拥有原生绘画。这提高了用户对软件的接受度,因为用户感觉"宾至如归"。也许你可以设计一种方式,不必为GTK用户去掉交替的背景。

最新更新