日期:2014-05-20  浏览次数:20810 次

如何给一个SWT的table设计条件格式??
就是某一行的特定列的数值符合某个条件,比如大于多少或者怎么样,就设置字体颜色


Java code

org.eclipse.swt.graphics.Color color = Display.getDefault().getSystemColor(SWT.COLOR_RED);
                    TableItem tti = new TableItem(table,SWT.NULL);
                    tti.setBackground(color);



像上面的代码是可以运行的,表格就新增加一红色背景的空行。


Java code
                    org.eclipse.swt.graphics.Color color = Display.getDefault().getSystemColor(SWT.COLOR_RED);
                    for(TableItem ti : table.getItems()){
                        if(Integer.parseInt(ti.getText(9))>20120101){
                            ti.setForeground(color);///////
                        }
                    }


但这样的代码没有效果,是不是已经存在的行不能改变??已经存在的行是数据库里面查询出来的。
另外的调试的时候已经执行到///////的那一行了

------解决方案--------------------
整个例子代码:
Java code

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();
            }
        }
    }

}