繁体   English   中英

即使在SWT中选择了TreeItem,如何强制实施TreeItem的前景色

[英]How to enforce the foreground color of a TreeItem even when it's selected in SWT

我按照这个例子来创建一个简单的测试,其中一个Table改变了前景色 这按预期工作。

但是,如果我将示例更改为使用Tree而不是Table ,则在选择项目时不会保留前景色。

在此代码段中,按下按钮时所选项目的前景色会更改为红色:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    shell.setText("StackOverflow");

    final Tree table = new Tree(shell, SWT.BORDER | SWT.MULTI);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for (int i = 0; i < 10; i++)
    {
        TreeItem item = new TreeItem(table, SWT.NONE);
        item.setText("item " + i);
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Color selected");

    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            List<TreeItem> allItems = new ArrayList<>(Arrays.asList(table.getItems()));
            TreeItem[] selItems = table.getSelection();

            for (TreeItem item : selItems)
            {
                item.setForeground(display.getSystemColor(SWT.COLOR_RED));
                allItems.remove(item);
            }

            for (TreeItem item : allItems)
            {
                item.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
            }
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

选择item0但颜色为黑色:
在此输入图像描述

但是如果我们取消选择该项目,我们可以看到前景颜色实际上已经改变:
在此输入图像描述

有谁知道为什么这在Table正常工作但在Tree没有? 有没有办法让这项工作?

这似乎与Windows上的表和树之间显示所选项目的方式略有不同。 可能唯一的解决方法是让你的应用程序绘制树项,如果你真的想要覆盖默认的选择颜色。

另请参阅此处的自定义绘图表和树项目文章: https//www.eclipse.org/articles/article.php?file = Article-CustomDrawingTableAndTreeItems/ index.html

暂无
暂无

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

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