RSS

Search Engine

Friday, June 11, 2010

Table

Create a new Java project "de.vogella.swt.table" with the swt.jar in the classpath. Create the following class.

			

package de.vogella.swt.table;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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 SWTTable {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);

shell.setLayout(new GridLayout());


Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 200;
table.setLayoutData(data);

String[] titles = { "First Name", "Last Name", "Age" };
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(titles[i]);
table.getColumn(i).pack();
}

for (int i = 0 ; i<= 50 ; i++){
TableItem item = new TableItem(table, SWT.NONE);
item.setText (0, "Person " +1 );
item.setText (1, "LastName " +1 );
item.setText (2, String.valueOf(i));
}

for (int i=0; i table.getColumn (i).pack ();
}
shell.pack ();
shell.open ();

shell.open();
while (!display.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}

If you run this application a table will be displayed.

0 comments:

Post a Comment