user674353
user674353

Reputation:

Java SWT: how to delete the selected row in a SWT table

I have implemented one SWT table having a button widget in one column. On click of a button I am deleting the entire row. But I don't understand how to refresh/redraw/update the table.

Table processListTable;
TableItem tableItem;
Image deleteImage = Activator.getImageDescriptor("icons/trash.gif").createImage();

private void addRowInTable() {
    tableItem = new TableItem(processListTable, SWT.FILL);
    tableItem.setText(0, "value 1");
    tableItem.setText(1, "value 2");

    TableEditor editor = new TableEditor(processListTable);

    final Button deleteButton = new Button(processListTable, SWT.PUSH | SWT.FILL);
    deleteButton.pack();

    editor.minimumWidth = deleteButtonButton.getSize().x;
    editor.horizontalAlignment = SWT.CENTER;
    editor.setEditor(deleteButtonButton, tableItem, 2);
    deleteButtonButton.setImage(deleteImage);
    deleteButtonButton.addListener(SWT.Selection, new SelectionListener(tableItem, checkButton));
}

class SelectionListener implements Listener {
    TableItem item;
    Button deleteButton;

    public SelectionListener(TableItem item, Button deleteButton) {
        this.item = item;
        this.deleteButton = deleteButton;
    }

    public void handleEvent(Event event) {

        this.deleteButton.dispose();
        this.item.dispose();
    }
}

Upvotes: 1

Views: 10545

Answers (3)

Kris
Kris

Reputation: 5792

Use the JFace TableViewer with a model class, delete the object from the model and refresh the TableViewer.

Upvotes: 1

user674353
user674353

Reputation:

public void handleEvent(Event event) {
    this.deleteButton.dispose();
    this.trash.dispose();
    this.item .dispose();

    Table table = viewer.getTable();
    table.getColumn(2).pack();
    table.getColumn(2).setWidth(100);
}

This is the solution for refresh the SWT table.

Upvotes: 1

Sorceror
Sorceror

Reputation: 4843

Check SWT snippet remove selected items from Table.

Just call table.remove(int rowIdx); instead of item.dispose();

Upvotes: 5

Related Questions