1

I was thinking if there's a way to apply a style like this onto a swt checkbox.
I looked for custom components, but didn't found anything.
Thank you guys

Luiz E.
  • 6,769
  • 10
  • 58
  • 98

2 Answers2

3

You can use the following: Create a custom button with SWT

as a starting point. Within the PaintListener you can paint the button in the way you want it to look.

Here is a small example i just tried:

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class ImageButton extends Canvas {
    private boolean checked = false;
    private final ImageButton button = this;

    public ImageButton(Composite parent, int style) {
        super(parent, style);

        this.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {
                if(checked)
                {
                    e.gc.drawImage(Icons.ON, 0, 0);
                }
                else
                {
                    e.gc.drawImage(Icons.OFF, 0, 0);
                }
                button.setSize(WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
            }
        });
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseUp(MouseEvent e) {
                checked = !checked;
                redraw();
            }
        });
    }
}

where Icons.ON and Icons.OFF are the two images and WIDTH_OF_IMAGE and HEIGHT_OF_IMAGE are the width and height of the image you use.

Community
  • 1
  • 1
Baz
  • 36,440
  • 11
  • 68
  • 94
2

OPAL widgets library has that kind of buttons.

http://code.google.com/a/eclipselabs.org/p/opal/wiki/SwitchButton