Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SWT Composite 1 px padding

Tags:

java

swt

I use a GridLayout on my parent Composite and want to kill the 1px padding that is created while rendering the object. What is the parameter to change to get this part working? My composite is rendered like this

final Composite note = new Composite(parent,SWT.BORDER);
GridLayout mainLayout = new GridLayout(1,true);
mainLayout.marginWidth = 0;
mainLayout.marginHeight = 0;
mainLayout.verticalSpacing = 0;
mainLayout.horizontalSpacing = 0;
note.setLayout(mainLayout);

Image:

enter image description here

like image 445
Johnny000 Avatar asked Oct 21 '13 14:10

Johnny000


1 Answers

SWT.BORDER is causing your issue. On Windows 7 it will draw a border of 2px, one grey and one white. Use SWT.NONE to get rid of the border altogether.

If you really want a 1px grey border, you can add a Listener for SWT.Paint to the parent of your Composite and make it draw a border with the GC:

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

    final Composite outer = new Composite(shell, SWT.NONE);
    outer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    GridLayout layout = new GridLayout(1, false);
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    outer.setLayout(layout);

    Composite inner = new Composite(outer, SWT.NONE);
    inner.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
    inner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    shell.addListener(SWT.Paint, new Listener()
    {
        public void handleEvent(Event e)
        {
            e.gc.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BORDER));
            Rectangle rect = outer.getBounds();
            Rectangle rect1 = new Rectangle(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2);
            e.gc.setLineStyle(SWT.LINE_SOLID);
            e.gc.fillRectangle(rect1);
        }
    });

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

Looks like this:

enter image description here

And here with green background:

enter image description here

like image 172
Baz Avatar answered Sep 22 '22 23:09

Baz