将鼠标侦听器添加到java swt图像

时间:2012-07-20 12:58:08

标签: java eclipse image swt listener

好的,我有这样的图像:

Image deleteImage = new Image(viewer.getTable().getDisplay(),
                        Provider.PATH_TO_DELETE_ICON);

如何添加鼠标监听器以便我知道何时单击图像?

这是我的标签提供者类:

private class MyLabelProvider extends LabelProvider implements
            ITableLabelProvider {

        @Override
        public Image getColumnImage(Object element, int columnIndex) {
            if (columnIndex == 3) {
                Image deleteImage = new Image(viewer.getTable().getDisplay(),
                        Provider.PATH_TO_DELETE_ICON);
                return deleteImage;
            }
            if (columnIndex == 4) {
                Image editImage = new Image(viewer.getTable().getDisplay(),
                        Provider.PATH_TO_EDIT_ICON);
                return editImage;
            } else
                return null;
        }

        public String getColumnText(Object element, int index) {
            Provider provider = (Provider) element;
            switch (index) {
            case 0:
                return Integer.toString(provider.getId());
            case 1:
                return provider.getName();
            case 2:
                return provider.getBulstat();
            default:
                return "";
            }
        }

    }

1 个答案:

答案 0 :(得分:6)

您可以创建Label,通过

将图像设置到其上
label.setImage(deleteImage)

之后您可以执行以下操作:

label.addMouseListener(new MouseListener() {

        @Override
        public void mouseUp(MouseEvent arg0) {
            System.out.println("Click");
        }

        @Override
        public void mouseDown(MouseEvent arg0) {

        }

        @Override
        public void mouseDoubleClick(MouseEvent arg0) {
        }
    });