import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
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;
/*
* Created on Jan 21, 2005
*
* TODO To change the template for this generated file go to Window -
Preferences - Java - Code
* Style - Code Templates
*/
/**
* @author mfkilgore
*
* TODO To change the template for this generated type comment go to
Window - Preferences - Java -
* Code Style - Code Templates
*/
public class TestTableDraw
{
private Table table;
protected Shell shell;
public static void main(String[] args)
{
try
{
TestTableDraw window = new TestTableDraw();
window.open();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void open()
{
final Display display = Display.getDefault();
createContents();
shell.layout();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
}
protected void createContents()
{
shell = new Shell();
shell.setLayout(new FormLayout());
shell.setSize(835, 686);
shell.setText("SWT Application");
table = new Table(shell, SWT.BORDER);
final FormData formData = new FormData();
formData.bottom = new FormAttachment(0, 565);
formData.right = new FormAttachment(0, 795);
formData.top = new FormAttachment(0, 50);
formData.left = new FormAttachment(0, 15);
table.setLayoutData(formData);
table.setLinesVisible(true);
table.setHeaderVisible(true);
final TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("New column");
final TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
tableColumn_1.setWidth(520);
tableColumn_1.setText("New column");
final TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
tableColumn_2.setWidth(179);
tableColumn_2.setText("New column");
final Button buttonNew = new Button(shell, SWT.NONE);
buttonNew.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
table.removeAll();
table.clearAll();
table.redraw();
for (int i = 0; i < 20; ++i)
{
StyledText widget = new StyledText(table, SWT.NONE);
widget.setText("This is a line of the table it is some
number of characters long - " + i);
StyleRange styleRange = new StyleRange();
styleRange.start = 0 + i;
styleRange.length = 5;
styleRange.fontStyle = SWT.BOLD;
widget.setStyleRange(styleRange);
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, "fixed");
TableEditor editor = new TableEditor(table);
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(widget, item, 1);
}
}
});
final FormData formData_1 = new FormData();
formData_1.bottom = new FormAttachment(0, 625);
formData_1.right = new FormAttachment(0, 190);
formData_1.top = new FormAttachment(0, 585);
formData_1.left = new FormAttachment(0, 40);
buttonNew.setLayoutData(formData_1);
buttonNew.setText("New Data");
final Button buttonClear = new Button(shell, SWT.NONE);
buttonClear.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
table.removeAll();
table.clearAll();
table.redraw();
}
});
final FormData formData_2 = new FormData();
formData_2.bottom = new FormAttachment(buttonNew, 0, SWT.BOTTOM);
formData_2.right = new FormAttachment(0, 370);
formData_2.top = new FormAttachment(buttonNew, 0, SWT.TOP);
formData_2.left = new FormAttachment(0, 240);
buttonClear.setLayoutData(formData_2);
buttonClear.setText("Clear Data");
}
}