Java SWT – List

List là đối tượng cho phép user lựa chọn 1 hoặc nhiều item trong 1 danh sách các item được liệt kê.

File ListEx.java

package vncoding;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;


public class ListEx {
    
    private Label status;

    public ListEx(Display display) {

        initUI(display);
    }

    private void initUI(Display display) {

        Shell shell = new Shell(display);

        status = new Label(shell, SWT.NONE);
        status.setText("Ready");

        FormLayout layout = new FormLayout();
        layout.marginHeight = 5;
        layout.marginWidth = 5;
        layout.spacing = 5;
        shell.setLayout(layout);

        FormData labelData = new FormData();
        labelData.left = new FormAttachment(0);
        labelData.right = new FormAttachment(100);
        labelData.bottom = new FormAttachment(100);
        status.setLayoutData(labelData);

        List list = new List(shell, SWT.BORDER);

        list.add("Tea");
        list.add("Coffee");
        list.add("Ice-cream");
        list.add("Lemon juice");
        list.add("Orange juice");
        list.add("Wine");

        list.addListener(SWT.Selection, event -> onListItemSelect(list));

        FormData listData = new FormData();
        listData.width = 250;
        listData.height = 200;
        listData.left = new FormAttachment(shell, 0);
        listData.top = new FormAttachment(shell, 0);
        listData.right = new FormAttachment(100, 0);
        listData.bottom = new FormAttachment(status, 0);
        list.setLayoutData(listData);

        shell.setText("List");
        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
    
    private void onListItemSelect(List list) {
        
        String[] items = list.getSelection();
        status.setText(items[0]);        
    }

    @SuppressWarnings("unused")
    public static void main(String[] args) {

        Display display = new Display();
        ListEx ex = new ListEx(display);
        display.dispose();
    }
}

Giải thích:
Trong ví dụ này, item được select và được hiển thị ở thanh statusbar.

status = new Label(shell, SWT.NONE);
status.setText("Ready");

Ở đây Label được sử dụng cho statusbar.

FormLayout layout = new FormLayout();
layout.marginHeight = 5;
layout.marginWidth = 5;
layout.spacing = 5;
shell.setLayout(layout);

Chúng ta sử dụng FormLayout để sắp xếp các đối tượng Label và List trên cửa sổ. Một vài thông số margin, heigh, width được set.

FormData labelData = new FormData();
labelData.left = new FormAttachment(0);
labelData.right = new FormAttachment(100);
labelData.bottom = new FormAttachment(100);
status.setLayoutData(labelData);

Đoạn code này add thêm statusbar ở cuối cửa sổ.

List list = new List(shell, SWT.BORDER);

List được tạo với tham số default là chỉ select được 1 item.

list.add("Tea");
list.add("Coffee");
list.add("Ice-cream");
list.add("Lemon juice");
list.add("Orange juice");
list.add("Wine");

Thêm item vào List.

list.addListener(SWT.Selection, event -> onListItemSelect(list));

Lắng nghe sự kiện khi user select item trong List. Khi 1 item được select, hàm onListItemSelect() được gọi.

FormData listData = new FormData();
listData.width = 250;
listData.height = 200;
listData.left = new FormAttachment(shell, 0);
listData.top = new FormAttachment(shell, 0);
listData.right = new FormAttachment(100, 0);
listData.bottom = new FormAttachment(status, 0);
list.setLayoutData(listData);

Chỉ định kích thước cho List

private void onListItemSelect(List list) {
    String[] items = list.getSelection();
    status.setText(items[0]);        
}

Hàm getSelection() trả về mảng tên item khi user select item đó. Tuy nhiên, trong ví dụ này, user chỉ select được 1 item nên chúng ta chỉ quan tâm tới phần tử đầu tiên items[0]. Tên item được hiển thị lên thanh status bằng hàm setText().

Kết quả:

Listbox
Listbox

Be the first to comment

Leave a Reply