开发者

Simultaneously right-align and wrap text in an SWT label in Linux/Gnome

I have a problem which appears when using Ubuntu Linux/Gnome, but seemingly not on Windows (as seen in one of the answers), that I can't use SWT.RIGHT | SWT.WRAP together, when creating a new label, in order to both make the text right-aligned and wrappable.

Background:

I'm using a simple 2-column Grid layout in a wizard, to create a dynamic number of rows with one label and one textfield per row. I want the labels to be of fixed size and with the text inside it right-aligned, like so:

      label 1: [Text field 1]
Another label: [Text field 2]
 Another label
  with wrapped [Text field 3]
         text: 

The gridlayout is created like so:

GridLayout gl = new GridLayout();
gl.numColumns = 2;
composite.setLayout(gl);

and this is how I try to create the rows (with one label and one textfield per row):

for (String labelText : labelTexts) {
    Label fieldLabel = new Label(this.composite, SWT.RIGHT | SWT.WRAP | SWT.BORDER );
    fieldLabel.setText(labelText);
    GridData labelGridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
    labelGridData.widthHint = 1开发者_运维技巧60;
    fieldLabel.setLayoutData(labelGridData);

    Text textField = new Text(this.composite, SWT.BORDER);
    GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
    textGridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
    textGridData.widthHint = 248;
    textField.setLayoutData(textGridData);
}

The problem is this part: SWT.RIGHT | SWT.WRAP above, since it does not work to use them both simultaneously. So how can I get around that, and both right-align the text in the labels, and make the text wrap?


I tried to recreate the problem on my Vista machine and using the eclipse 3.6. But it worked for me.

Here is the output:

Simultaneously right-align and wrap text in an SWT label in Linux/Gnome

The code I have used is as follows:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class RightAlign 
{
    public static void main(String[] args)
    {

        Display display = new Display();
        Shell shell = new Shell(display);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        shell.setLayout(layout);
        shell.setText("Alignment Test");

        String[] labelTexts = new String[]{"label 1:","Another label:","Another label with wrapped text:"};

        for (String labelText : labelTexts) 
        {
            Label  fieldLabel = new Label(shell,  SWT.RIGHT |SWT.WRAP | SWT.BORDER );
            fieldLabel.setText(labelText);
            GridData labelGridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
            labelGridData.widthHint = 100;
            fieldLabel.setLayoutData(labelGridData);

            Text textField = new Text(shell, SWT.BORDER);
            GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
            textGridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
            textGridData.widthHint = 248;
            textField.setLayoutData(textGridData);
        }

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

    }
}

The only thing I changed is the labelGridData.widthHint from 160 to 100. The longest label was < 160 and hence was not getting wrapped.


Short answer:

Use the StyledText instead of the Label - all things equal.

Simultaneously right-align and wrap text in an SWT label in Linux/Gnome

Detailed answer:

There are a number of bugs (120256 being a good entry point) in GTK about this "feature" most of them marked duplicate of each other, most of them harking back to gtk+ 2.4/2.6 some marked fixed. However, in 2011 gtk2+ version 2.22 (for Maverick Market), bugs seem to be remarkably resilient.

Having read this bug report, I grepped the whole SWT source tree for the wordwrap keyword and it looks like Steve Northover boys have worked around the bug much better with the StyledText Control than with the Label Control.

I reused Favonius test case (thx a lot Favonius ;-), and that apparently was good omen indeed.

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class RightAlign2
{
    public static void main(String[] args)
    {

        Display display = new Display();
        Shell shell = new Shell(display);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        shell.setLayout(layout);
        shell.setText("Alignment Test");

        String[] labelTexts = new String[]{"label 1:","Another label:","Another label with wrapped text:"};

        for (String labelText : labelTexts)
        {
            StyledText  fieldText = new StyledText(shell, SWT.RIGHT |SWT.WRAP | SWT.BORDER );
            fieldText.setText(labelText);
            GridData labelGridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
            labelGridData.widthHint = 100;
            fieldText.setLayoutData(labelGridData);

            Text textField = new Text(shell, SWT.BORDER);
            GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
            textGridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
            textGridData.widthHint = 248;
            textField.setLayoutData(textGridData);
        }

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

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜