How to make background labels transparent in SWT

I'm new to java SWT and while putting labels it looks like I can't make a transparent label a label. I am using Eclipse Luna SWT desine form. this is what my window looks like: enter image description here

as you can see, all the backgrounds of the labels are white. What should I do to keep the background of the labels the same as the default window background (gray) (the value is not by changing the background of each label at runtime, which works for me).

it's probably simple, but I just couldn't google how to do it. here is my code:

   import java.awt.Color;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Canvas;


public class WinApp2 {
    private DataBindingContext m_bindingContext;

    protected Shell shell;
    private Text destenation_ip;
    private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
    private Label lblDestenationPortIp;
    private Label lblNewLabel;
    private Text ammount_of_packets;
    private Label lblNewLabel_1;
    private Text sorce_ip;
    private Text destenation_port;
    private Text sorce_port;
    private Label lblNewLabel_2;
    private Label label;
    private Label label_1;
    private Label ammount_of_packets_info;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        Display display = Display.getDefault();
        Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
            public void run() {
                try {
                    WinApp2 window = new WinApp2();
                    window.open();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();

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

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
        shell.setSize(450, 306);
        shell.setText("LAN Destroyer");
        shell.setLayout(new FormLayout());
        destenation_ip = new Text(shell, SWT.BORDER);
        FormData fd_destenation_ip = new FormData();
        destenation_ip.setLayoutData(fd_destenation_ip);

        lblDestenationPortIp = new Label(shell, SWT.NONE);
        lblDestenationPortIp.setAlignment(SWT.RIGHT);
        FormData fd_lblDestenationPortIp = new FormData();
        fd_lblDestenationPortIp.left = new FormAttachment(0, 351);
        fd_lblDestenationPortIp.right = new FormAttachment(100, -10);
        fd_lblDestenationPortIp.top = new FormAttachment(0, 35);
        lblDestenationPortIp.setLayoutData(fd_lblDestenationPortIp);
        lblDestenationPortIp.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
        formToolkit.adapt(lblDestenationPortIp, true, true);
        lblDestenationPortIp.setText(":\u05E9\u05DC \u05D9\u05E2\u05D3 IP");

        lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setAlignment(SWT.RIGHT);
        FormData fd_lblNewLabel = new FormData();
        fd_lblNewLabel.right = new FormAttachment(100, -10);
        lblNewLabel.setLayoutData(fd_lblNewLabel);
        formToolkit.adapt(lblNewLabel, true, true);
        lblNewLabel.setText(":\u05DB\u05DE\u05D5\u05EA");

        ammount_of_packets = new Text(shell, SWT.BORDER);
        fd_destenation_ip.left = new FormAttachment(ammount_of_packets, 0, SWT.LEFT);
        fd_destenation_ip.right = new FormAttachment(ammount_of_packets, -1, SWT.RIGHT);
        FormData fd_ammount_of_packets = new FormData();
        fd_ammount_of_packets.right = new FormAttachment(lblNewLabel, -68);
        fd_ammount_of_packets.top = new FormAttachment(lblNewLabel, -3, SWT.TOP);
        ammount_of_packets.setLayoutData(fd_ammount_of_packets);
        formToolkit.adapt(ammount_of_packets, true, true);

        Button nonstop_send = new Button(shell, SWT.CHECK);
        fd_ammount_of_packets.left = new FormAttachment(0, 186);
        nonstop_send.setAlignment(SWT.RIGHT);
        nonstop_send.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(nonstop_send.getSelection()){
                    ammount_of_packets_info.setText("שולח ללא הפסקה");
                    ammount_of_packets_info.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
                    ammount_of_packets.setEnabled(false);
                }
                else {
                    ammount_of_packets_info.setText("");
                    ammount_of_packets.setEnabled(true);
                }

            }
        });
        FormData fd_nonstop_send = new FormData();
        fd_nonstop_send.top = new FormAttachment(lblNewLabel, 0, SWT.TOP);
        fd_nonstop_send.right = new FormAttachment(ammount_of_packets, -9);
        nonstop_send.setLayoutData(fd_nonstop_send);
        formToolkit.adapt(nonstop_send, true, true);
        nonstop_send.setText("\u05E9\u05DC\u05D7 \u05DC\u05DC\u05D0 \u05D4\u05E4\u05E1\u05E7\u05D4");

        lblNewLabel_1 = new Label(shell, SWT.NONE);
        fd_lblNewLabel.top = new FormAttachment(lblNewLabel_1, 6);
        lblNewLabel_1.setAlignment(SWT.RIGHT);
        FormData fd_lblNewLabel_1 = new FormData();
        fd_lblNewLabel_1.right = new FormAttachment(100, -10);
        fd_lblNewLabel_1.top = new FormAttachment(lblDestenationPortIp, 6);
        lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
        formToolkit.adapt(lblNewLabel_1, true, true);
        lblNewLabel_1.setText(":\u05E9\u05DC \u05DE\u05E7\u05D5\u05E8 IP");

        sorce_ip = new Text(shell, SWT.BORDER);
        fd_destenation_ip.bottom = new FormAttachment(sorce_ip);
        fd_lblNewLabel_1.left = new FormAttachment(sorce_ip, 29);
        FormData fd_sorce_ip = new FormData();
        fd_sorce_ip.top = new FormAttachment(0, 58);
        fd_sorce_ip.left = new FormAttachment(0, 186);
        fd_sorce_ip.right = new FormAttachment(100, -114);
        sorce_ip.setLayoutData(fd_sorce_ip);
        formToolkit.adapt(sorce_ip, true, true);

        destenation_port = new Text(shell, SWT.BORDER);
        fd_nonstop_send.left = new FormAttachment(destenation_port, 0, SWT.LEFT);
        FormData fd_destenation_port = new FormData();
        fd_destenation_port.top = new FormAttachment(0, 32);
        fd_destenation_port.left = new FormAttachment(0, 40);
        destenation_port.setLayoutData(fd_destenation_port);
        formToolkit.adapt(destenation_port, true, true);

        sorce_port = new Text(shell, SWT.BORDER);
        sorce_port.setForeground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
        sorce_port.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
        FormData fd_sorce_port = new FormData();
        fd_sorce_port.top = new FormAttachment(lblNewLabel_1, -3, SWT.TOP);
        fd_sorce_port.left = new FormAttachment(destenation_port, 0, SWT.LEFT);
        sorce_port.setLayoutData(fd_sorce_port);
        formToolkit.adapt(sorce_port, true, true);

        lblNewLabel_2 = new Label(shell, SWT.NONE);
        FormData fd_lblNewLabel_2 = new FormData();
        fd_lblNewLabel_2.top = new FormAttachment(0, 35);
        fd_lblNewLabel_2.left = new FormAttachment(destenation_port, 6);
        lblNewLabel_2.setLayoutData(fd_lblNewLabel_2);
        formToolkit.adapt(lblNewLabel_2, true, true);
        lblNewLabel_2.setText(":\u05E4\u05D5\u05E8\u05D8");

        label = new Label(shell, SWT.NONE);
        label.setText(":\u05E4\u05D5\u05E8\u05D8");
        FormData fd_label = new FormData();
        fd_label.bottom = new FormAttachment(nonstop_send, -6);
        fd_label.left = new FormAttachment(sorce_port, 6);
        label.setLayoutData(fd_label);
        formToolkit.adapt(label, true, true);

        label_1 = new Label(shell, SWT.WRAP | SWT.SHADOW_IN);
        FormData fd_label_1 = new FormData();
        fd_label_1.top = new FormAttachment(ammount_of_packets, 20);
        fd_label_1.left = new FormAttachment(0, 187);
        label_1.setLayoutData(fd_label_1);
        formToolkit.adapt(label_1, true, true);
        label_1.setText(":\u05E1\u05D9\u05DB\u05D5\u05DD");

        ammount_of_packets_info = new Label(shell, SWT.NONE);
        ammount_of_packets_info.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_MAGENTA));
        FormData fd_ammount_of_packets_info = new FormData();
        fd_ammount_of_packets_info.left = new FormAttachment(100, -158);
        fd_ammount_of_packets_info.top = new FormAttachment(ammount_of_packets, 44);
        fd_ammount_of_packets_info.right = new FormAttachment(100, -36);
        ammount_of_packets_info.setLayoutData(fd_ammount_of_packets_info);
        formToolkit.adapt(ammount_of_packets_info, true, true);
        m_bindingContext = initDataBindings();

    }
    protected DataBindingContext initDataBindings() {
        DataBindingContext bindingContext = new DataBindingContext();
        //
        return bindingContext;
    }
}

      

thank!

+3


source to share


2 answers


Your problem is triggered FormToolkit#adapt(Control, boolean, boolean)

. Internally, this method sets the background color to the background color of the toolbox.

You can prevent this by calling:

formToolkit.setBackground(null);

      




Link: SourceFormToolkit#adapt(Control, boolean, boolean)

+7


source


If you still want to have a text, use what I have mentioned above: shell.setBackground(SWT.INHERIT_FORCE);

. This makes it so that the background of your label matches the background color.

However, you can also do a simpler option. If you want to "hide" the text, but need to save it later and display it whenever you want, you can set the text of a specific label as null

.



I use this a lot when I need text to disappear. The only thing you need is to save the text to an array (list) - or whatever you want (i.e. Database or something similar) so that you can save the text later. You don't even need to use SWT.INHERIT_FORCE

. This is a complete example that might help:

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;


/**
 *
 * @author Dylan
 */

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));
        shell.setText("Stackoverflow Test");

        shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
//        shell.setBackgroundMode(SWT.INHERIT_FORCE);

        Button button = new Button(shell, SWT.PUSH);
        button.setText("Button that fun to click - but there is no text :D");
        Label label = new Label(shell, SWT.NONE);
        label.setText("Horray, you entered text to stdin!!");

        ArrayList<String> labelText = new ArrayList<>();
        labelText.add(label.getText());
        label.setText("");

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

        Boolean flag = false;
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
                if (!flag) {
                    try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
                        System.out.println("Please enter some text");
                        char[] cbuf = new char[40];

                        String text = reader.readLine();

                        if (text.length()> 0) {
                            flag = true;

                            label.setText(labelText.remove(0));
                            button.setText(button.getText() + "... and now there is. Yay!");
                            shell.pack();
                        } else {
                            break;
                        }
                    } catch (IOException io) {
                        System.err.println(io.toString());
                    }
                }
            }
        }

        display.dispose();
    }

}

      

+1


source







All Articles