Java SWT – Spinner

Spinner là đối tượng điều khiển cho phép lựa chọn giá trị nằm trong giới hạn. Bạn có thể tăng, giảm bằng click chuột up/down hoặc sử dụng keyboard Arrow up/ Arrow down, Page up / Page down.

package vncoding;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

public class SpinnerEx {
    
    private Label label;
    
    public SpinnerEx(Display display) {

        initUI(display);
    }

    private void initUI(Display display) {    
        
        Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
        
        RowLayout layout = new RowLayout();
        layout.marginLeft = 10;
        layout.marginTop = 10;
        layout.spacing = 30;
        layout.center = true;
        shell.setLayout(layout);
        
        Spinner spinner = new Spinner(shell, SWT.BORDER);
        spinner.setMinimum(0);
        spinner.setMaximum(100);
        spinner.setSelection(0);
        spinner.setIncrement(1);
        spinner.setPageIncrement(10);       
        spinner.setLayoutData(new RowData(30, -1));
        
        spinner.addListener(SWT.Selection, event -> onSelected(spinner));
        
        label = new Label(shell, SWT.NONE);
        label.setText("0");
        
        shell.setText("Spinner");
        shell.setSize(200, 150);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }        
    }
    
    private void onSelected(Spinner spinner) {
        
        String val = spinner.getText();
        label.setText(val);
        label.pack();
    }
    
    @SuppressWarnings("unused")
    public static void main(String[] args) {

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

Giải thích:

Trong ví dụ này, sử dụng 2 đối tượng Label và Spinner. Giá trị được lựa chọn từ Spinner được hiển thị trên Label.

Spinner spinner = new Spinner(shell, SWT.BORDER);

Tạo đối tượng spinner với thuộc tính có border

spinner.setMinimum(0);
spinner.setMaximum(100);
spinner.setSelection(0);
spinner.setIncrement(1);
spinner.setPageIncrement(10); 

Chỉ đỉnh giá trị min, max, giá trị khởi tạo, bước nhảy (khi click chuột hoặc Arrow up/down), bước nhảy (khi Page up/down)

spinner.addListener(SWT.Selection, event -> onSelected(spinner));

Lắng nghe sự kiện khi user select Spinner. Khi Spinner được select (click hoặc keyboard), hàm onSelected() được gọi.

private void onSelected(Spinner spinner) {
    String val = spinner.getText();
    label.setText(val);
    label.pack();
}

Hàm getText() trả về giá trị hiện tại của Spinner. Giá trị được hiển thị lên Label bởi hàm setText().

Kết quả:

Spinner
Spinner

Be the first to comment

Leave a Reply