How to set SWT label padding?

11,830

Solution 1

Layout (such as GridLayout) and LayoutData (e.g. GridData) objects in SWT can only control spacing outside a control (so they may only set margins, not padding). In order to change control side itself you can only use setSize() and setBound().

Solution 2

(Answer many years later, but still an answer...)

CLabel cl = new CLabel(shell, SWT.CENTER);
int padding = 5;
cl.setMargins(padding, padding, padding, padding);

Solution 3

Quoting my answer to a similar question:

I also wrestled with this issue. Label does not support padding. I wound up using StyledText instead.

   final StyledText text = new StyledText(parent, SWT.WRAP);
   final int padding = 5;

   text.setLeftMargin(padding);
   text.setRightMargin(padding);
   text.setTopMargin(padding);
   text.setBottomMargin(padding);

   text.setWordWrap(true);
   text.setCaret(null);

This did the trick for me.

Share:
11,830
kostja
Author by

kostja

We are here to learn :) Works and plays with scala, kafka, data, linux

Updated on June 04, 2022

Comments

  • kostja
    kostja almost 2 years

    When i assign text to my Labels, they wrap around it very tightly, sometimes cutting the lower edges off 'p', 'y' and alike. I would like to have some padding between text and border. I am using a TableWrapLayout for the parent Composite and TableWrapData for the Labels

        TableWrapLayout layout = new TableWrapLayout();
        layout.numColumns = 2;
        layout.bottomMargin = 10;
        layout.topMargin = 10;
        client.setLayout(layout);
    
        Label label= toolkit.createLabel(client, "", SWT.NONE);
    

    We are using the FormToolkit for consistent design, IMHO this has no influence on border painting