简体   繁体   English

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

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

I follow this example to create a simple test with a Table where the foreground color was changed . 我按照这个例子来创建一个简单的测试,其中一个Table改变了前景色 This works as expected. 这按预期工作。

But if I change the example to use a Tree instead of a Table , the foreground color is not kept when I select the item. 但是,如果我将示例更改为使用Tree而不是Table ,则在选择项目时不会保留前景色。

In this code snippet, the foreground color of selected item is changed to red when the button is pressed: 在此代码段中,按下按钮时所选项目的前景色会更改为红色:

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 is selected but color is black: 选择item0但颜色为黑色:
在此输入图像描述

but if we deselect the item we can see the foreground color was actually changed: 但是如果我们取消选择该项目,我们可以看到前景颜色实际上已经改变:
在此输入图像描述

Does anyone know why this works correctly in Table but not in Tree ? 有谁知道为什么这在Table正常工作但在Tree没有? Is there a way to make this work? 有没有办法让这项工作?

This seems to be a slight difference in how selected items are shown, between table and tree on Windows. 这似乎与Windows上的表和树之间显示所选项目的方式略有不同。 Probably the only workaround is to let your application draw the tree items if you really want to override the default selection colors. 可能唯一的解决方法是让你的应用程序绘制树项,如果你真的想要覆盖默认的选择颜色。

See also the Custom drawing table and tree items article here: https://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html 另请参阅此处的自定义绘图表和树项目文章: 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