Java SWT Composite 1 px padding

I am using GridLayout for my parent Composite and want to kill the 1px debugging that is created when the object is rendered. What is the parameter for changing the operation of this part? My composite is displayed as follows

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);

Picture

enter image description here

+4
source share
1 answer

SWT.BORDERcauses your problem. On Windows 7, a 2px border will be drawn, one gray and one white. Use SWT.NONEto completely get rid of the border.

If you really want a 1px gray border, you can add Listenerfor SWT.Paintto your parent element Compositeand make it draw a border with 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();
}

It looks like:

enter image description here

And here with a green background:

enter image description here

+7

All Articles