openplanning

Hướng dẫn và ví dụ Java SWT Spinner

Xem thêm các chuyên mục:

Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi người.
Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.
Hãy theo dõi chúng tôi trên Fanpage để nhận được thông báo mỗi khi có bài viết mới. Facebook

1- SWT Spinner

Trong SWT, Spinner cho phép người dùng lựa chọn các giá trị trong một tập các giá trị số. Thật đáng tiếc SWT Spinner không cho phép bạn lựa chọn các giá trị Object.

2- Ví dụ với Spinner

Ví dụ đơn giản dưới đây minh họa một Spinner với các giá trị số.
SpinnerDemo.java

package org.o7planning.swt.spinner;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
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 SpinnerDemo {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT Spinner (o7planning.org)");

        RowLayout rowLayout = new RowLayout();
        rowLayout.marginLeft = 10;
        rowLayout.marginTop = 10;
        rowLayout.spacing = 15;
        shell.setLayout(rowLayout);
        // Label
        Label label = new Label(shell, SWT.NONE);
        label.setText("Select Level:");

        // Spinner
        final Spinner spinner = new Spinner(shell, SWT.BORDER);
        spinner.setMinimum(1);
        spinner.setMaximum(5);
        spinner.setSelection(3);

        Font font = new Font(display, "Courier", 20, SWT.NORMAL);
        spinner.setFont(font);

        // Label
        Label labelMsg = new Label(shell, SWT.NONE);
        labelMsg.setText("?");
        labelMsg.setLayoutData(new RowData(150, SWT.DEFAULT));
        spinner.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                labelMsg.setText("You select: " + spinner.getSelection());
            }
        });
        shell.setSize(400, 250);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

Xem thêm các chuyên mục: