1. Code Snippet to associate a Delete Key Listener to a treeViewer.
class DeleteKeyListener implements KeyListener
{
@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.DEL) {
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
[widgetObj].addKeyListener(new DeleteKeyListener ());
For ex - treeViewer.addKeyListener(new DeleteKeyListener ());
2. Complete Code Snippet for implementing a Delete Key Listener to Delete a table or tree row.
class DeleteModifierKeyListener implements KeyListener
{
@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.DEL) {
IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
if (selection.isEmpty()) {
return;
} else {
TreeItem[] items = treeViewer.getTree().getSelection();
for (TreeItem treeItem : items)
{
list.remove(treeItem.getData());
}
treeViewer.refresh();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}