简体   繁体   English

删除SWT TreeItem图像缩进

[英]Remove SWT TreeItem image indent

I am trying to build an SWT Tree that has icons at the top level but not at the next level. 我正在尝试构建一个SWT树,它在顶层有图标但在下一层没有。

树截图

Is there any way to avoid the blank space which seems to have been left for the image which I'm not using? 有没有办法避免似乎留给我不使用的图像的空白区域? I tried using the following code snippets but neither did what I wanted. 我尝试使用以下代码片段,但没有做我想要的。

SWT.MeasureItem: SWT.MeasureItem:

tree.addListener(SWT.MeasureItem, new Listener()
{
  @Override
  public void handleEvent(Event event)
  {
    TreeItem item = (TreeItem)event.item;
    Image image = item.getImage();
    if (image == null)
    {
      event.x -= 40;
    }
  }
});

SWT.PaintItem: SWT.PaintItem:

tree.addListener(SWT.PaintItem, new Listener()
{
  @Override
  public void handleEvent(Event event) {
    TreeItem item = (TreeItem)event.item;
    Image image = item.getImage();
    if (image == null)
    {
      event.x -= 40;
    }
  }
});

In both cases I was just hoping that the text could be drawn a bit further to the left. 在这两种情况下,我只是希望文本可以在左侧进一步绘制。

This behavior comes from the native controls and is OS-specific (AFAIR, on Macs you won't see this problem). 此行为来自本机控件,并且是特定于操作系统的(AFAIR,在Mac上您不会看到此问题)。 Alas, no easy fix but add some generic icon (or not adding icons at all). 唉,没有简单的修复,但添加一些通用图标(或根本不添加图标)。

I have done some more investigation myself. 我自己做了一些调查。 As per Eugene's answer this seems to be native behaviour. 根据尤金的回答,这似乎是本土行为。 There are a couple of things worth noting. 有几件值得注意的事情。

If no items in the Tree have an icon then no space is left for icons. 如果树中的项目没有图标,则图标不会留下任何空间。 However, even a single item with an icon will cause all items to leave space for icons. 但是,即使带有图标的单个项目也会导致所有项目留出图标空间。

A hacky solution can be implemented as follows: hacky解决方案可以实现如下:

  • Use no icons so that the native control leaves no icon space 不使用图标,以便本机控件不会留下任何图标空间
  • For items where you want an icon prefix some spaces to the text eg " " + text 对于您想要图标前缀的项目,文本中有一些空格,例如“”+文本
  • Add a PaintItem listener that draws the icon you want into the space left by the text 添加一个PaintItem侦听器,将所需的图标绘制到文本留下的空间中

This probably doesn't work well across platforms and across system fonts so I've just decided to live with having icons. 这可能不适用于跨平台和跨系统字体,所以我决定忍受有图标。

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

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