public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.DIALOG_TRIM);
shell.setBounds(100,100,500,400);
final Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setBounds(100,100, 300, 200);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setWidth(100);
column1.setResizable(false);
column1.setText("Item");
TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setWidth(100);
column2.setResizable(false);
column2.setText("Controls");
Listener buttonListener = new Listener() {
public void handleEvent(Event event) {
final Button button = (Button) event.widget;
if(button == null) {
return;
}
Rectangle buttonBounds = button.getBounds();
Rectangle clientArea = table.getClientArea();
Point point = new Point( buttonBounds.x , buttonBounds.y );
int index = table.getTopIndex();
while (index < table.getItemCount()) {
TableItem item = table.getItem(index);
Rectangle cellArea = item.getBounds(0);
if(! cellArea.intersects(clientArea)){
return;
}
if(cellArea.contains(point)){
System.out.println("Item "+ index + " is selected");
return;
}
index++;
}
}
};
for(int i=0; i<10; i++){
TableItem tableItem = new TableItem(table, SWT.NONE );
if ((i%2) == 0){
tableItem.setText(0,"Item " + Integer.toString(i));
tableItem.setText(1,"Text " );
} else {
tableItem.setText(0,"Text " + Integer.toString(i));
TableEditor editor = new TableEditor(table);
tableItem.setData(editor);
Button button = new Button(table, SWT.CHECK);
button.setText("Button");
button.setSelection(false);
button.pack();
editor.minimumWidth = table.getColumn(0).getWidth();
editor.horizontalAlignment = SWT.LEFT;
editor.setEditor(button, tableItem, 1);
button.addListener(SWT.Selection, buttonListener);
}
}
Button deleteButton = new Button(shell, SWT.PUSH);
deleteButton.setBounds(420, 100, 60, 20);
deleteButton.setText("Delete");
deleteButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent se){
int index = table.getSelectionIndex();
if(index == -1) return;
TableItem item = table.getItem(index);
TableEditor editor = (TableEditor) item.getData();
if(editor != null){
Button button = (Button) editor.getEditor();
if(button != null){
button.dispose();
}
editor.dispose();
}
table.remove(index);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}