Take the 2-minute tour ×

In my SWT application I have a canvas showing a map currently i draw directly to the canvas some representation of data. I want to achieve functionality like in Swing where i can have a ImageIcon which can manage its on click listening and add these over the canvas, how can this be achieved, how does one add some transparently over a canvas so i can still click the canvas, and which classes are like ImageIcon? Currently I must iterate to check if a click matches the location at which i have drawn a point this is undesirable.

share|improve this question

1 Answer 1

You can add another (transparent) canvas to your canvas and listen for mouse events on it.

org.eclipse.swt.graphics.Image dataImage = ...;

Canvas dataCanvas = new Canvas(mapCanvas, SWT.TRANSPARENT);
dataCanvas.addPaintListener(new PaintListener(){
    public void paintControl(PaintEvent e) {
        e.gc.drawImage(dataImage, 0, 0);
dataCanvas.setBounds(locationX, loctionY,
    dataImage.getBounds().width, dataImage.getBounds().height);

share|improve this answer
Thats my inefficient solution just moved to another canvas. I have solved this myself and will post later. –  dominic Mar 16 '12 at 12:26
@denis Well, your question certainly describes something else. –  Martti Käärik Mar 16 '12 at 13:02
"currently i draw directly to the canvas some representation of data" so you just moved that to another canvas and overlaid it and as images are not clickable i would still have my original problem of looping to check for clicks on target areas. –  dominic Mar 16 '12 at 13:29
@denis No, the whole point of adding another canvas to draw the image on is that canvas is "clickable" (you will make the additional canvas to be the same size with the image that it displays). –  Martti Käärik Mar 16 '12 at 14:03
I understand how it would work but it achieves absolutely nothing not in my initial very poor solution. I understand that you had trouble gauging what i was asking and i can accept my share of that but accept that this not a good solution, its what i already had on another canvas. The canvas is clickable IMAGES ARE NOT as already said twice, so you are left with my original looping solution to check if i actually clicked in a region of interest. I have solved this myself using labels. –  dominic Mar 16 '12 at 14:47

Your Answer


