简体   繁体   English

SWT表列偏移/填充

[英]SWT Table column offset/padding

I have an SWT Table with multiple columns. 我有一个带有多列的SWT Table In this example, let's say there are three columns. 在此示例中,假设有三列。 The cells of each column all contain an image followed by some text (example pictured below). 每列的单元格都包含一个图像,后跟一些文本(示例如下图所示)。 However, as you can see there is no spacing between the 2 - 3 column lines and their cells. 但是,如您所见,2-3列线与其单元之间没有间距。

Is there a built-in way to add a buffer zone to those cells so the icons don't appear right on the edge line? 是否有内置的方法向这些单元格添加缓冲区,以使图标不会出现在边缘线上? Such as an offset property of some kind? 例如某种偏移属性? I don't see any property overtly listed in either Table or TableColumn . 我没有在TableTableColumn公开列出任何属性。

If not, is there a workaround beyond just adding white space to the cell images? 如果没有,除了将空白添加到单元格图像之外,还有其他解决方法吗?

Please let me know if there's anything I can do to make my question clearer. 如果可以采取任何措施使我的问题更清楚,请告诉我。

Table: 表:

表

I don't think there is a designated way to adjust the margin and spacing of image and text within a cell. 我认为没有一种指定的方法可以调整单元格中图像和文本的边距和间距。 Apart from adding transparent pixels to the image (as you already suggested), you can use a PaintListener to gain control over how a cell is rendered. 除了向图像添加透明像素(如您已经建议的那样)之外,您还可以使用PaintListener来控制如何渲染单元格。

The example below draws image and text with adjustable margin and spacing: 下面的示例绘制具有可调边距和间距的图像和文本:

Listener paintListener = new Listener() {
  int leftMargin = 40;
  int rightMargin = 10;
  int imageSpacing = 200;
  @Override
  public void handleEvent( Event event ) {
    TableItem item = ( TableItem )event.item;
    Rectangle imageBounds = image.getBounds();
    Point textExtent = event.gc.textExtent( item.getText() );
    switch( event.type ) {
      case SWT.MeasureItem: {
        event.width += leftMargin + imageBounds.width + imageSpacing + textExtent.x + rightMargin;
        event.height = Math.max( event.height, imageBounds.height + 2 );
        event.height = Math.max( event.height, textExtent.y + 2 );
        break;
      }
      case SWT.PaintItem: {
        int x = event.x + leftMargin;
        int imageOffset = ( event.height - imageBounds.height ) / 2;
        event.gc.drawImage( image, x, event.y + imageOffset );
        x += imageSpacing;
        int textOffset = ( event.height - textExtent.y ) / 2;
        event.gc.drawText( item.getText(), x, event.y + textOffset );
        break;
      }
      case SWT.EraseItem: {
        event.detail &= ~SWT.FOREGROUND;
      }
    }
  }
};
table.addListener( SWT.MeasureItem, paintListener );
table.addListener( SWT.PaintItem, paintListener );
table.addListener( SWT.EraseItem, paintListener );

For a more thorough understanding of owner-drawn items in SWT please read the Custom Drawing Table and Tree Items article. 为了更全面地了解SWT中所有者绘制的项目,请阅读“ 自定义工程图表和树项目”文章。

If you are using a JFace TableViewer , there is also an OwnerDrawLabelProvider as shown in this example: http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html#styledcelllabelprovider-and-ownerdrawlabelprovider 如果您使用的是JFace TableViewer ,则也有一个OwnerDrawLabelProvider ,如以下示例所示: http : //www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html#styledcelllabelprovider-and-ownerdrawlabelprovider

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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