The problem of placing SWT - is it possible to fill in the labels?

I am using GridLayout in my GUI application GUI. I have the following GridData defined for each grid cell. The grid cell itself is just a label.

GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; gridData.heightHint = 25; gridData.widthHint = 25; gridData.verticalAlignment = GridData.VERTICAL_ALIGN_CENTER; gridData.verticalIndent = 10; 

And I create each table element like this -

  Label l = new Label(shell, SWT.BORDER); l.setAlignment(SWT.CENTER); l.setText("some text"); l.setLayoutData( gridData ); 

Now my problem, despite using the verticalAlignment property, the verticalIndent and setAlignment properties on the label itself, I can’t get the text aligned vertically with respect to the grid cell area (25 x 25). I think something is missing. How to achieve vertical alignment in a grid cell?

+4
source share
3 answers

There is no setVerticalAlignment () on the label, and if you set the height using heightHint in LayoutData, this will become the size of the control and actually stop you from setting it in the middle. A workaround would be to put the Label in the component with the right size, and then center the control. For instance.

 Composite labelCell = new Composite(shell, SWT.BORDER); GridData gridData = new GridData(); gridData.heightHint = 250; gridData.widthHint = 250; labelCell.setLayoutData(gridData); labelCell.setLayout(new GridLayout()); Label l = new Label(labelCell , SWT.NONE); l.setText("some text"); l.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true)); 
+4
source

CLabel? Why don't you use it?

 CLabel cl = new CLabel(shell, SWT.CENTER); cl.setBounds(0, 0, 100, 100); cl.setText("Custom Label!!!"); 

Even, you can use leftMargin, rightMargin, topMargin, bottomMargin.

(Sorry for three years later)

+8
source

I also struggled with this problem. Label does not support add-ons. Instead, I used StyledText .

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

It helps me.

+1
source

All Articles