1

I want to implement selecting capability in my custom control. This control contains an image and I want image to turn bluish when selected. I don't want to change image bitwise.

Is it possible to draw some half-transparent blue rectangle above other controls to simulate the selection?

UPDATE

The following code shows no any drawing:

public class ImageWithCaption extends Composite {

    private Label imageLabel;
    private Label textLabel;

    private void init() {
        setLayout(new GridLayout(1, false));

        imageLabel = new Label(this, SWT.NONE);
        imageLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

        textLabel = new Label(this, SWT.NONE);
        textLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

        addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {

                GC gc = e.gc;


                gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE));
                gc.fillRectangle(0, 0, e.width, e.height);

            }
        });
    }

    public ImageWithCaption(Composite parent, int style) {
        super(parent, style);
        init();

    }

    public void setText(String text) {
        textLabel.setText(text);
    }

    public String getText() {
        return textLabel.getText();
    }

    public Image getImage() {
        return imageLabel.getImage();
    }

    public void setImage(Image image) {
        imageLabel.setImage(image);
    }




}
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • 1
    Your best option would be to listen for `SWT.Paint`, then draw your `Image` with the [`GC`](http://help.eclipse.org/indigo/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/GC.html) and on top, draw a semi-transparent rectangle (using `GC#setAlpha(int)`). Read [this](http://eclipse.org/articles/Article-SWT-graphics/SWT_graphics.html) for more details – Baz Feb 17 '14 at 14:59
  • But how to draw over composite? My drawings seem to appear below other controls constituting composite. – Suzan Cioc Feb 17 '14 at 15:27
  • 1
    You have to add the listener to the `Label` holding the image, not the parent `Composite`. – Baz Feb 17 '14 at 15:30
  • But is it possible to cover entire composite somehow? – Suzan Cioc Feb 17 '14 at 15:32

1 Answers1

0

transparency is a difficult subject with SWT...

there is an Overlay from Opal

Or you could look into Appkit

I myself went with the overlay from Opal and extended it to get some extra functonality.

Flummiboy
  • 591
  • 1
  • 7
  • 20