2017-01-16 1 views
1

나는이 example to create a simple test with a Table where the foreground color was changed을 따른다. 이것은 예상대로 작동합니다.SWT에서 선택된 경우에도 TreeItem의 전경색을 적용하는 방법

Table 대신 Tree을 사용하는 예제를 변경하면 항목을 선택할 때 전경색이 유지되지 않습니다.

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에서는 올바르게 작동하지 않습니까? 이 일을 할 수있는 방법이 있습니까?

답변

0

이것은 Windows에서 테이블과 트리 사이에 선택된 항목이 표시되는 방식에 약간의 차이가있는 것으로 보입니다. 아마도 유일한 대안은 기본 선택 색상을 실제로 덮어 쓰려는 경우 응용 프로그램에서 트리 항목을 그릴 수있게하는 것입니다.

참조 여기에 또한 사용자 정의 드로잉 테이블과 트리 항목 기사 : https://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html

관련 문제