简体   繁体   中英

How to add a String containing a line break to a swt list

I have a GUI with a org.eclipse.swt.widgets.List in it and would like to add a String that contains a line break to it. However, it does not work and the line break is ignored. Can anyone help? Here is my code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class Test {

public static void main(String[] args) {

    Display display = new Display();
    final Shell myShell = new Shell(display);
    myShell.setText("MyShell");
    myShell.setLayout(new GridLayout(1, false));

    Composite composite = new Composite(myShell, SWT.MULTI);
    composite.setLayout(new GridLayout(1, false));
    GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd_composite.widthHint = 250;
    composite.setLayoutData(gd_composite);

    List mylist = new List(composite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
    mylist.add("my" + "\n" + "String"); 
    myShell.setSize(94, 94);
    myShell.open();
    while (!myShell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}
}

The String is displayed in the list as myString . However, I'd like to display it like this:

my

String

You can't have line breaks in SWT List widgets. You can however use a Table with just one column instead and make the text in the cell wrap/break.

There are examples here:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM