日期:2014-05-20 浏览次数:20847 次
org.eclipse.swt.graphics.Color color = Display.getDefault().getSystemColor(SWT.COLOR_RED); TableItem tti = new TableItem(table,SWT.NULL); tti.setBackground(color);
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);/////// } }
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(); } } } }