How to set a control to a transparent background
Asked Answered
F

4

9

How do I set the background of a control to be transparent?

I am speaking of Label and Text controls at the moment, but can be any of the standard controls that I see in the GUI.

Furrier answered 7/1, 2014 at 15:50 Comment(2)
Why do you need them to be transparent?Comanchean
Visual Studio makes labels transparent by default, as does other environments that I worked on. The answer is that I do not want to set a background color. I want the background of the window (application background) to show through.Furrier
C
10
shell.setBackgroundMode(SWT.INHERIT_FORCE);

will do what you want.

The Composite constant to indicate that an attribute (such as background) is inherited by all children.

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    shell.setText("StackOverflow");

    shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
    shell.setBackgroundMode(SWT.INHERIT_FORCE);

    new Button(shell, SWT.PUSH).setText("Button");
    new Label(shell, SWT.NONE).setText("Label");

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }

    display.dispose();
}

Looks like this:

enter image description here

Comanchean answered 7/1, 2014 at 16:7 Comment(1)
Nice answer. I just had to add this one line: shell.setBackgroundMode(SWT.INHERIT_FORCE); after I set my background.Furrier
E
2

According to my knowledge you can't set a control (except of a shell on some operating systems) transparent or semitransparent in SWT, e.g. show a panel in front of a table control where the table will show through the panel. As the other posters wrote, one only can inherit the background.

Enuresis answered 27/4, 2015 at 6:20 Comment(0)
C
2

If you add a Composite and specify the following flags, it will be transparent: new Composite(shell, SWT.TRANSPARENT | SWT.NO_BACKGROUND);

Coltson answered 13/2, 2018 at 14:42 Comment(0)
F
0

You need to make the following call on your composite before child controls, such as Labels will inherit the background from the composite.

composite.setBackgroundMode( SWT.INHERIT_DEFAULT );
Foreworn answered 7/1, 2014 at 16:0 Comment(1)
I added the line of code along with other code to create the composite variable (see my update) and still I do not see the labels as transparent.Furrier

© 2022 - 2024 — McMap. All rights reserved.