[英]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();
}
但是如果我們取消選擇該項目,我們可以看到前景顏色實際上已經改變:
有誰知道為什么這在Table
正常工作但在Tree
沒有? 有沒有辦法讓這項工作?
這似乎與Windows上的表和樹之間顯示所選項目的方式略有不同。 可能唯一的解決方法是讓你的應用程序繪制樹項,如果你真的想要覆蓋默認的選擇顏色。
另請參閱此處的自定義繪圖表和樹項目文章: https : //www.eclipse.org/articles/article.php?file = Article-CustomDrawingTableAndTreeItems/ index.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.