deckard cain
deckard cain

Reputation: 497

How to change SWT Button background color or make it transparent

I have a transparent image on a Button (no text), which is placed on a Composite. Since the Composite is white (created with FormToolkit#createComposite(parent, SWT.NONE)), I'd like the Button background to be the same color. How do I do it?

The Label does the trick, but doesn't have the shadows like Button does when I'm clicking on it..

Upvotes: 1

Views: 4877

Answers (1)

avojak
avojak

Reputation: 2360

The background color of a Button is determined by the OS. In fact, the documentation for Control.setBackground() states that:

Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed.

That said, one possible way to circumvent this is to override the paint event as shown here: Changing org.eclipse.swt.widgets background color in Windows. When I tried this out the results were a bit wonky.

The safest and most consistent approach would be to use a label like in your second image, but have different images to display on various mouse events to emulate how a button behaves.

Those images can emulate the shadow just by adding whatever shape of shadow you want to the image itself. That shadow can also change for each image to give the impression that the button is being pressed or not.

For example, I'm thinking something along the lines of:

public class MyButton { 

    private final Label buttonLabel;

    public MyButton(final Composite parent, final Theme theme) {
        buttonLabel = new Label(parent, SWT.NONE);
        buttonLabel.setImage(theme.getUpImage());
        buttonLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDown(final MouseEvent mouseEvent) {
                buttonLabel.setImage(theme.getButtonPressedImage());
            }
            @Override
            public void mouseUp(final MouseEvent mouseEvent) {
                buttonLabel.setImage(theme.getButtonUpImage());
            }
        });
        buttonLabel.addMouseTrackListener(new MouseTrackAdapter() {
            @Override
            public void mouseEnter(final MouseEvent mouseEvent) {
                buttonLabel.setImage(theme.getButtonHoverImage());
            }
            @Override
            public void mouseExit(final MouseEvent mouseEvent) {
                buttonLabel.setImage(theme.getButtonUpImage());
            }
        });
    }

}

Where the Theme just has all of the images already conveniently loaded.

You'll also need to make sure that the parent Composite has the background mode set to force its background color:

parent.setBackgroundMode(SWT.INHERIT_FORCE);

Obviously the drawback to this approach is that you have to handle the mouse click logic yourself (ie. mouseDown isn't really clicked until the mouse is released, so you'll have to handle the state of the button in each listener method).

Upvotes: 1

Related Questions