package de.tutorials;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
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 TableSaver {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("TableSaver");
shell.setLayout(new FillLayout());
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new GridLayout(1, false));
final Table table = new Table(comp, SWT.MULTI | SWT.BORDER
| SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
for (int i = 0; i < 5; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Col" + i);
column.setWidth(50);
}
for (int i = 0; i < 10; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { i + "A", i + "B", i + "C", i + "D",
i + "E", });
}
Button btnSave = new Button(comp, SWT.PUSH);
btnSave.setText("Save Tabledata");
btnSave.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("selected: " + System.currentTimeMillis());
Display.getCurrent().asyncExec(new Runnable() {
public void run() {
TableItem[] items = table.getItems();
final int cols = table.getColumnCount();
try {
final BufferedWriter bw = new BufferedWriter(
new FileWriter("c:/tableOutput.txt"));
for (int i = 0; i < items.length; i++) {
final TableItem item = items[i];
for (int j = 0; j < cols; j++) {
String str = item.getText(j);
bw.write(str);
bw.write(";");
}
bw.newLine();
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
}
}