View on GitHub

Eclipse Tutorial

Ecsoya

online tutorialsWIKIoffline tutorialsPPT

List Tutorial

List和上一篇讲过的列表式(SWT.SIMPLE)Combo类似,都是用来从一堆选项中选择可用项的。不过List分为单选的(SWT.SINGLE)可多选的(SWT.MULTI)两类。

闲话少说,引例为证:

    Display display = new Display();
    Shell shell = new Shell(display);

    shell.setText("List Tutorial");
    shell.setSize(300, 200);

    shell.setLayout(new RowLayout(SWT.VERTICAL));

    List list1 = new List(shell, SWT.SINGLE | SWT.BORDER);
    for (int i = 0; i < 5; i++) {
        list1.add("single-selection-" + i);
    }
    list1.select(2);

    List list2 = new List(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    list2.setLayoutData(new RowData(SWT.DEFAULT, 60));
    for (int i = 0; i < 5; i++) {
        list2.add("multi-selection-" + i);
    }
    list2.select(2, 3);

    shell.open();

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

如图:


需要注意的地方


事件监听

1. DefaultSelection通过addSelectionListener(SelectionListener)或addListener(SWT.DefaultSelection, Listener)来添加。

触发条件当List中的某一项被双击时,立即触发

2. Selection通过addSelectionListener(SelectionListener)或addListener(SWT.Selection, Listener)来添加。

触发条件当List中的选择项发生变化时,立即触发

    Display display = new Display();
    Shell shell = new Shell(display);

    shell.setText("List Tutorial");
    shell.setSize(300, 200);

    shell.setLayout(new GridLayout());

    final Text text = new Text(shell, SWT.MULTI | SWT.V_SCROLL
            | SWT.H_SCROLL);
    text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final List list = new List(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    list.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
    for (int i = 0; i < 5; i++) {
        list.add("multi-selection-" + i);
    }
    list.select(2, 3);
    list.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String[] selection = list.getSelection();
            text.setText("Selected items: " + Arrays.toString(selection));
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            String[] selection = list.getSelection();
            text.setText(text.getText() + "\nDouble clicked on: "
                    + Arrays.toString(selection));
        }
    });

    shell.open();

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

参考资料: * List snippets * 如果想了解更多的关于设置颜色,字体等其它属性的相关内容,请移步至控件的通用设置 * 如果想了解更多的关于LayoutLayoutData的相关内容,请移步至布局管理器 * SWT Example: ControlExample * Sample code and further information


Back to Home 上一篇:Combo Tutorial 下一篇:Scale Tutorial