本帖最后由 p2227 于 2012-02-28 16:12:31 编辑

解决方案 »

  1.   

    在个例子上加了个判断,不知道是不是你要的效果
    判断:if (i == 5) {
    item.setForeground(color);
    }
      

  2.   

    整个例子代码:import org.eclipse.jface.dialogs.MessageDialog;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.MouseEvent;
    import org.eclipse.swt.events.MouseListener;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.layout.RowLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    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 Table1 { /**
     * @param args
     */
    public static void main(String[] args) {
    final Display display = Display.getDefault();
    final Shell shell = new Shell();
    shell.setLayout(new FillLayout());
    /**
     * 创建 一个Table对象,在式样里设置它可多选、全列选择。 并用两条语句设置显示表头和表格线
     */
    final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);// org.eclipse.swt.graphics.Color color = Display.getDefault().getSystemColor(SWT.COLOR_RED);
    // table.setBackground(color);


    /**
     * 为表格增加两列并设置列宽为80
     */
    TableColumn col1 = new TableColumn(table, SWT.NONE);
    col1.setText("ID");
    col1.setWidth(80); TableColumn col2 = new TableColumn(table, SWT.NONE);
    col2.setText("NAME");
    col2.setWidth(80);
    /**
     * 创建一个Composite容器,并放入两个按钮
     */
    Composite composite = new Composite(shell, SWT.NONE);
    composite.setLayout(new RowLayout());
    Button addButton = new Button(composite, SWT.NONE);
    addButton.setText("新增");

    addButton.addSelectionListener(new SelectionAdapter() {
    org.eclipse.swt.graphics.Color color = Display.getDefault().getSystemColor(SWT.COLOR_RED);
    int i = 1;
    TableItem item = null;

    @Override
    public void widgetSelected(SelectionEvent e) {
    item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "" + i, "CSDN" + i });
    if (i == 5) {
    item.setForeground(color);
    }
    i++;
    }

    }); Button removeButton = new Button(composite, SWT.NONE);
    removeButton.setText("删除");
    removeButton.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
    // 得到装有所有被选择记录的序号的数组
    int[] sellines = table.getSelectionIndices();
    // Table 根据数组中的序号将Item删除
    table.remove(sellines);
    }
    }); /**
     * 监听Table的鼠标双击事件
     */
    table.addMouseListener(new MouseListener() { @Override
    public void mouseUp(MouseEvent arg0) {
    // TODO Auto-generated method stub } @Override
    public void mouseDown(MouseEvent arg0) {
    // TODO Auto-generated method stub } @Override
    public void mouseDoubleClick(MouseEvent arg0) {
    int selIndex = table.getSelectionIndex();
    TableItem item = table.getItem(selIndex);
    String str = "列1 = " + item.getText(0) + "\n 列2 = "
    + item.getText(1);
    MessageDialog.openInformation(null, null, str);
    }
    }); shell.layout();
    shell.open();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
    display.sleep();
    }
    }
    }}
      

  3.   

    还是一样啊,item = new TableItem(table, SWT.NONE);
    这样子就能修改,通过查询数据库出来的数据就不能改