2014-09-26 61 views
0

我想在我的List上选择它们之后更改项目的颜色。 默认颜色是蓝色的,这是我的语法:SWT在列表中选择后的项目颜色

org.eclipse.swt.widgets.List list = new org.eclipse.swt.widgets.List(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); 

list.setBounds(1250, 160, 200, 300); 
for(int i=0;i<40;i++) { 
    list.add("asasd"); 
} 
+1

不它需要成为一个列表?使用无标题的单列表格很容易。 – 2014-09-26 13:26:07

+0

@ThomasS。是的,它需要成为一个清单 – Alex 2014-09-26 13:49:59

+1

@Alex为什么?你能详细说明吗?因为否则答案将是“否”(或者说“并非没有大量的工作”)。 – Baz 2014-09-26 13:52:25

回答

2

在这里,你走了,带着一个Table的解决方案:

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

    // If you choose to create a Color instance yourself, remember to dispose() it! 
    final Color highlight = display.getSystemColor(SWT.COLOR_YELLOW); 

    final Table table = new Table(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER); 

    table.addListener(SWT.EraseItem, new Listener() 
    { 
     public void handleEvent(Event event) 
     { 
      event.detail &= ~SWT.HOT; 
      if ((event.detail & SWT.SELECTED) == 0) 
       return; ///item not selected 

      Table table = (Table) event.widget; 
      int clientWidth = table.getClientArea().width; 

      GC gc = event.gc; 
      Color oldForeground = gc.getForeground(); 
      Color oldBackground = gc.getBackground(); 

      gc.setBackground(highlight); 
      gc.fillRectangle(0, event.y, clientWidth, event.height); 

      gc.setForeground(oldForeground); 
      gc.setBackground(oldBackground); 
      event.detail &= ~SWT.SELECTED; 
     } 
    }); 

    final TableColumn column = new TableColumn(table, SWT.NONE); 

    table.addListener(SWT.Resize, new Listener() 
    { 
     @Override 
     public void handleEvent(Event event) 
     { 
      column.setWidth(table.getClientArea().width); 
     } 
    }); 

    for (int i = 0; i < 10; i++) 
     new TableItem(table, SWT.NONE).setText(Integer.toString(i)); 

    shell.pack(); 
    shell.open(); 

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

是这样的:

enter image description here

相关问题