I'm a new user of SWT.
I've a problem with elimination of a row. In the third colum there are some buttons.
I've modified a snippet. This is my code:
- Code: Select all
package swtsnippets;
/*
* Table example snippet: remove selected items (using popup menu)
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.swt.*;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
public class Snippet53 {
public static void main (String [] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final Table table = new Table(shell, SWT.MULTI | SWT.BORDER
| SWT.FULL_SELECTION);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 200;
table.setLayoutData(data);
table.setLinesVisible(true);
table.setHeaderVisible(true);
createColumns(table);
for (int i=0; i<8; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText (0,"Item " + i);
item.setText (1,"Cane " + i);
Button c = new Button(table, SWT.PUSH);
c.setText("B"+i);
c.setEnabled(true);
TableEditor editor = new TableEditor(table);
editor.grabHorizontal = editor.grabVertical = true;
editor.setEditor(c, item, 2);
// editor.setEditor(null, item, 2);
item.setData("IO", editor);
}
Menu menu = new Menu (shell, SWT.POP_UP);
table.setMenu (menu);
MenuItem item = new MenuItem (menu, SWT.PUSH);
item.setText ("Power Off");
item.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
TableItem ti = table.getItem(table.getSelectionIndex());
TableEditor b = (TableEditor)ti.getData("IO");
redispose(table,b);
table.remove (table.getSelectionIndex());
}
});
resizeColumns(table);
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
private static void redispose(Table table, TableEditor b) {
Control [] controls = table.getChildren();
System.out.println("->"+controls.length);
for (int i = 0; i < controls.length; i++) {
if(controls[i].equals(b.getEditor())){
controls[i].setVisible(false);
controls[i].dispose();
System.out.println("Bingo");
}
}
}
private static void resizeColumns(Table table) {
for (int i=0; i<3; i++) {
table.getColumn (i).pack ();
System.out.println(table.getHeaderHeight());
}
}
private static void createColumns(Table table) {
// Create five columns
TableColumn column = new TableColumn(table, SWT.CENTER);
column.setText("Prima");
column = new TableColumn(table, SWT.CENTER);
column.setText("MAC Address");
column = new TableColumn(table, SWT.CENTER);
column.setText("Parked");
}
}
I'm trying to do various test without any resoult. I've also tried to hide the button but i've some refresh problem that I'm not able to resolve. The solution is to eliminate the button but I accept any solutions.
Thanks.
Andrea.