Skip to content

SWT Tutorial : Checkbox Button Example

package com.deveshcodelab.examples.swt;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SWTSnippet04 {

	public static void main(String[] args) {

		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout(1, true));

		Button button = new Button(shell, SWT.CHECK | SWT.BORDER);
		button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		button.setText("Click Me");
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MessageDialog.openInformation(shell, "Info",
						"Button State: " + (button.getSelection() ? "CHECKED" : "UNCHECKED"));

			}
		});

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

	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *