Sunday 8 January 2017

SWT: Message box

SWT provides MessageBox class to define MessageBox widget.

Ex:
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);
messageBox.setMessage("Are you male?");
messageBox.setText("MessageBoxDemo");

You can create MessageBox with following styles.

ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING
OK, OK | CANCEL
YES | NO, YES | NO | CANCEL
RETRY | CANCEL
ABORT | RETRY | IGNORE

Note:
a. Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING and ICON_WORKING may be specified.

Following is the complete working application.
package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {
  shell.open();
  MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);
  messageBox.setMessage("Are you male?");
  messageBox.setText("MessageBoxDemo");
  int response = messageBox.open();

  switch (response) {
  case SWT.YES:
   System.out.println("Good Mornining Mr..");
   break;
  case SWT.NO:
   System.out.println("Good Morning Ms");
   break;
  case SWT.CANCEL:
   System.out.println("The user cancelled.");
   break;
  }

 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);
  shell.setSize(500, 500);
  addWidgetsToShell(display, shell);

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

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

  /* Dispose the display */
  display.dispose();

 }
}


Run above application, you can able to see following screen.



Previous                                                 Next                                                 Home

No comments:

Post a Comment