SWT Table(Table是无法编辑的,若要创建出可编辑的table,需要自行使用Composite与GirdLayout来开发) 1.创建简单的Table 创建Table就是创建出Table类的对象,传入parent容器与样式属性。对该对象加入TableColumn这个类的对象来表示想要出现在table中的字段。
Table t=new Table(s,SWT.BORDER);
创建table字段主要有三个步骤: 1)对每个字段创建出TableColumn的对象。 2)使用SetText()对每个字段设置表头文字。 3)使用SetWidth()来设定每个字段的宽度。
TableColumn tc1=new TableColumn(t,SWT.CENTER); TableColumn tc2=new TableColumn(t,SWT.CENTER); TableColumn tc3=new TableColumn(t,SWT.CENTER); tc1.setText("First Name"); tc2.setText("Last Name"); tc3.setText("Address"); tc1.setWidth(75); tc2.setWidth(70); tc3.setWidth(80); t.setHeaderVisible(true);
TableColumn支持三种样式:SWT.CENTER、SWT.RIGHT、SWT.LEFT,这些样式决定了文字在每个字段中显示方式。
创建完Table的表头后,需要通过TableItem类来加入数据到Table中。
TableItem item1=new TableItem(t,SWT.NONE); item1.setText(new String[]{"Tim","Hatton","Kentucky"}); TableItem item2=new TableItem(t,SWT.NONE); item2.setText(new String[]{"Caitlyn","Warner","Ohio"}); TableItem item3=new TableItem(t,SWT.NONE); item3.setText(new String[]{"Reese","Miller","Ohio"}); 每个TableItem对象代表了table中的一行且行中的每一栏都有一个值。数据是通过String数组加入TableItem中的。
2.字段表头与网格线的显示 字段表头的显示: t.setHeaderVisible(true); 网格线的显示: t.setLinesVisible(true);
3.强调选取行 在行中的某一栏被选取时,通过将跨字段的整行强调来给用户以整行被选取的视觉指示,可以使用SWT.FULL_SELECTION这个样式即可。
4.允许选取多行 Table默认情况下只被允许选取一行。可以设定SWT.MULTI样式来允许选取多行。在选取时必须通过按住ctrl键来进行多行选取。
5.程序化得选取项目 若table的项目是可以被选取的,那就一定有方法来判断哪个项目被选取的,或者可以在没有用户介入的情况下让某个项目被选取。 Table通过getSelection()这个方法返回当前被选取的TableItem(一行数据)对象的数组。 TableItem []ti=t.getSelection(); 若Table是多重选取的,可以通过getSelectionIndices()返回int值的数组来以零基准索引指出哪些TableItem被选取。 int[] selected=t.getSelectionIndices(); 若Table为单选的,可以通过getSelectionIndex()返回当前被选取项目的索引。 int selected=t.getSelectionIndex();
6.Check样式 可以通过SWT.CHECK样式在table项目前加上一个checkbox,可以通过setChecked()方法来让项目成为checked,通过getChecked()方法判断项目的状态。 item3.setChecked(true); boolean checked=item3.getChecked();
7.改变背景颜色
通过使用setBackground()来指定个别TableItem的背景颜色:item2.setBackground(new Color(d,127,178,127));
8.创建可以搜素和替换的Table 虽然Table这个类内置了通过键入cell中的文字的第一个字符来寻找项目的功能,但这还不足以提供完整的Table搜索能力。还可以通过getText()和setText()对TableItem中的文字进行交互,完成寻找与替换功能。 import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*;
public class TableShell { Display d; Shell s; public TableShell(){ d=new Display(); s=new Shell(d); s.setSize(250,200); s.setText("Table Shell"); GridLayout g1=new GridLayout(); g1.numColumns=4; s.setLayout(g1); //create a table final Table t=new Table(s,SWT.BORDER|SWT.FULL_SELECTION|SWT.CHECK|SWT.MULTI); //create table header final GridData gd=new GridData(GridData.FILL_BOTH); gd.horizontalSpan=4; t.setLayoutData(gd); TableColumn tc1=new TableColumn(t,SWT.LEFT); TableColumn tc2=new TableColumn(t,SWT.CENTER); TableColumn tc3=new TableColumn(t,SWT.CENTER); tc1.setText("First Name"); tc2.setText("Last Name"); tc3.setText("Address"); tc1.setWidth(75); tc2.setWidth(70); tc3.setWidth(80); t.setHeaderVisible(true); t.setLinesVisible(true); //create table cell elements final TableItem item1=new TableItem(t,SWT.NONE); item1.setText(new String[]{"Tim","Hatton","Kentucky"}); TableItem item2=new TableItem(t,SWT.NONE); item2.setText(new String[]{"Caitlyn","Warner","Ohio"}); TableItem item3=new TableItem(t,SWT.NONE); item3.setText(new String[]{"Reese","Miller","Ohio"}); final Text find=new Text(s,SWT.SINGLE|SWT.BORDER); final Text replace=new Text(s,SWT.SINGLE|SWT.BORDER); final Button b=new Button(s,SWT.PUSH|SWT.BORDER); b.setText("replace"); b.addSelectionListener(new SelectionAdapter(){ public void widgetSelected(SelectionEvent e){ TableItem[] tia=t.getItems(); for(int i=0;i<tia.length;i++){ //搜索并替换第三列的数值 if(tia[i].getText(2).equals(find.getText())) tia[i].setText(2,replace.getText()); } } }); s.open(); while(!s.isDisposed()) if(!d.readAndDispatch()) d.sleep(); d.dispose(); } public static void main(String args[]){ new TableShell(); } }
要想将Table中的整行以新值取代旧值,可以再setText()中传入String对象的数组。数组中的每个元素会被用来替代调用setText()的TableItem中的单一cell: String [] newText={"Nikki","Miller","Asia"}; item2.setText(newText);
本文来自博客,转载请标明出处:http://blog.csdn.net/freesnail/archive/2009/05/19/4202529.aspx