CODEXE

SWT message box

swt message dialog

SWT Message Box

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;


public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Display d = new Display();
		Shell s = new Shell(d);
		s.setSize(200, 100);
		s.setLayout(new FillLayout(SWT.VERTICAL));
		//TODO: Add UI
		Button btn = new Button(s, SWT.NONE);
		btn.setText("Show...");
		btn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				MessageBox dialog =
						new MessageBox(s, SWT.ICON_ERROR | SWT.OK| SWT.CANCEL);
				dialog.setText("Error");
				dialog.setMessage("Do you really want to continue?");
				int res = dialog.open();
			}
		});
		//UI End
		s.open();

		while (!s.isDisposed()) {
		  if (!d.readAndDispatch())
			d.sleep();
		}
		d.dispose();
	  
	}
}
			 

The code above is to show an Error dialog. If you want to show different dialog such as Info dialog and Warning dialog, just change the SWT.ICON_ERROR to SWT.ICON_INFORMATION and SWT.ICON_WARNING respectively

swt message box