humansg
humansg

Reputation: 715

Eclipse SWT label not showing within Composite

My label is not showing up as expected in the codes below without setting the bound. I am creating a Composite within the shell as I only want the background image to appear within this composite.

In this case what should the bound be? Can I get an optimal bound based on the text of the label?

Display display = PlatformUI.createDisplay();
Shell shell = new Shell(display);
shell.setText("Header);

Composite main = new Composite(shell, SWT.NONE);
main.setBounds(10, 5, 775, 505);
InputStream is = getClass().getResourceAsStream("/resources/bg.png");
Image bg = new Image(display, is);
main.setBackgroundImage(bg);        
main.setBackgroundMode(SWT.INHERIT_DEFAULT);

Label label = new Label(main, SWT.NONE);            
//label.setBounds(0, 0, 400,100);           // not showing if commented away
label.setText("Label 1");

Upvotes: 2

Views: 3868

Answers (1)

Baz
Baz

Reputation: 36904

This works for me on Linux (Eclipse 3.6.2, Java 1.6.0.26):

public class StackOverflow
{
    public static void main(String[] args)
    {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        shell.setText("Header");

        Composite main = new Composite(shell, SWT.NONE);
        main.setLayout(new GridLayout(1, false));
        Image bg = new Image(display, "resources/bg.png");
        main.setBackgroundImage(bg);
        main.setBackgroundMode(SWT.INHERIT_DEFAULT);

        Label label = new Label(main, SWT.NONE);    
        label.setLayoutData(new GridData(GridData.FILL_BOTH));
        label.setText("Label 1");

        main.pack();
        main.setBounds(10, 5, 775, 505);
        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
}

Upvotes: 3

Related Questions