现在已经实现选中一列中的任意一项都可以改变整列的背景色,然而这是系统自己的颜色,我什么改成自己想要的颜色
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;/**
 * 
 * 
 * 
 */
public class TableSelection { public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.MULTI);
table.setHeaderVisible(true);
table.setLinesVisible(true);
final TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Column 1");
final TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Column 2");
table.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Rectangle clientArea = table.getClientArea();
//table.getRegion();
Point pt = new Point(event.x, event.y);
int index = table.getTopIndex();
//int a=table.getItemCount();
//System.out.print(a);
while (index < table.getItemCount()) {
boolean visible = false;
TableItem item = table.getItem(index);
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
table.setSortColumn(table.getColumn(i));
//table.GET
//table.setBackground( new Color(""));
     table.setSortDirection(( SWT.DOWN));   
String ss=table.getColumn(i).getText();
System.out.print(i+"----"+ss);
//item.setBackground(new Color(null, 0, 255, 0));
}
if (!visible && rect.intersects(clientArea)) {
visible = true;
}
}
if (!visible)
return;
index++;
}
}
}); Listener sortListener = new Listener() {
public void handleEvent(Event e) {
TableColumn column = (TableColumn) e.widget;
table.setSortColumn(column);
}
};
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "a", "3" });
item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "b", "2" });
item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "c", "1" });
column1.setWidth(100);
column2.setWidth(100);
column1.addListener(SWT.Selection, sortListener);
column2.addListener(SWT.Selection, sortListener);
table.setSortColumn(column1);
table.setSortDirection(SWT.UP);
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}}

解决方案 »

  1.   

    列选中的颜色,稍微复杂一点。因为最直接提供的是针对行的Provider。
    其实也好解决,就是把每一行的那一个cell,改变它的状态,
    swt会遍历一下每一个Provider,然后提供给正确的颜色,我这么说你明白不?
      

  2.   


    改变 table.getColumn(i) 状态?
      

  3.   

    这个状态不一定是swt提供你的,
    你的程序结构,最好要形成一种典型的数据和视图分离的模型。
    也就是,每一个cell的数据(包含它的背景色在内),要和显示分开。
    然后refresh你的table,就会调用每一个cell的Provider了这样你就有机会change那些cell的color了。
      

  4.   

    晕 我运行了你的程序 给我报错了~~
    Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-gtk-3550 or swt-gtk in swt.library.path, java.library.path or the jar file
      

  5.   

    ? 我这几天做Notes插件呢,和Eclipse是一样的。
    再发你一篇文章,看看你能用上不?
    http://www.ibm.com/developerworks/lotus/library/notes8-sidebar/
      

  6.   


    import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Table;
    import org.eclipse.swt.widgets.TableColumn;
    import org.eclipse.swt.widgets.TableItem;
      
    public class TableEditoExamples {   
    public static void main(String[] args) {

            Display display = new Display();
            final Color blue = display.getSystemColor ( SWT.COLOR_BLUE ) ; 
            Shell shell = new Shell(display);
            shell.setLayout(new FillLayout());
            final Table table = new Table(shell, SWT.MULTI);
            table.setHeaderVisible(true);
            table.setLinesVisible(true);
            final TableColumn column1 = new TableColumn(table, SWT.NONE);
            column1.setText("Column 1");
            final TableColumn column2 = new TableColumn(table, SWT.NONE);
            column2.setText("Column 2");
            table.addListener(SWT.MouseDown, new Listener() {
                public void handleEvent(Event event) {
                    Rectangle clientArea = table.getClientArea();
                    //table.getRegion();
                    Point pt = new Point(event.x, event.y);
                    int index = table.getTopIndex();
                    //int a=table.getItemCount();
                    //System.out.print(a);
                    while (index < table.getItemCount()) {
                        boolean visible = false;
                        TableItem item = table.getItem(index);
                        for (int i = 0; i < table.getColumnCount(); i++) {
                            Rectangle rect = item.getBounds(i);
                            if (rect.contains(pt)) {
                                table.setSortColumn(table.getColumn(i));
                                //table.GET                            
                                //table.setBackground( new Color(""));
                                 table.setSortDirection(( SWT.DOWN));   
                                String ss=table.getColumn(i).getText();
                                System.out.print(i+"----"+ss);
                                //item.setBackground(new Color(null, 0, 255, 0));
                            }
                            if (!visible && rect.intersects(clientArea)) {
                                visible = true;
                            }
                        }
                        if (!visible)
                            return;
                        index++;
                    }
                }
            });        TableItem item = new TableItem(table, SWT.NONE);
            item.setText(new String[] { "a", "3" });
            item = new TableItem(table, SWT.NONE);
            item.setText(new String[] { "b", "2" });
            item = new TableItem(table, SWT.NONE);
            item.setText(new String[] { "c", "1" });
            column1.setWidth(100);
            column2.setWidth(100);
            Listener sortListener = new Listener() {
                public void handleEvent(Event e) {
                    TableColumn column = (TableColumn) e.widget;
                    table.setSortColumn(column);
                    table.getItem(0).setBackground(0,blue);
                    table.getItem(1).setBackground(0,blue);
                    table.getItem(2).setBackground(0,blue);
                }
            };
            column1.addListener(SWT.Selection, sortListener);
            column2.addListener(SWT.Selection, sortListener);
            table.setSortColumn(column1);
            table.setSortDirection(SWT.UP);
            shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            display.dispose();
        }
      
    }