Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a control to a transparent background

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.

like image 937
Sarah Weinberger Avatar asked Jan 07 '14 15:01

Sarah Weinberger


3 Answers

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

like image 113
Baz Avatar answered Sep 17 '22 13:09

Baz


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.

like image 33
Thomas S. Avatar answered Sep 17 '22 13:09

Thomas S.


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

like image 38
thSoft Avatar answered Sep 19 '22 13:09

thSoft