gpt4 book ai didi

java - 即使在 SWT 中选择了 TreeItem,如何强制执行 TreeItem 的前景色

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:30 25 4
gpt4 key购买 nike

我关注这个example to create a simple test with a Table where the foreground color was changed 。这按预期工作。

但是,如果我将示例更改为使用 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 已选择,但颜色为黑色:
enter image description here

但是如果我们取消选择该项目,我们可以看到前景色实际上已更改:
enter image description here

有谁知道为什么这在Table中可以正常工作,但在Tree中却不能?有办法让它工作吗?

最佳答案

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

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

关于java - 即使在 SWT 中选择了 TreeItem,如何强制执行 TreeItem 的前景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41676375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com