9.1. Creating Message Boxes
Problem
You need to send a message to the user and/or get a little feedback from the user.
Solution
Use the SWT MessageBox class to create a
message box, and check the return value
from its open method to determine which button the
user clicked.
Discussion
Here are the MessageBox class’s
methods:
-
String getMessage( ) Returns the dialog’s message
-
int open( ) Makes the dialog visible
-
void setMessage(String string) Sets the dialog’s message
You create a message box by passing the current shell to the
MessageBox constructor, as well as the style you
want to use: MessageBox(Shell parent, int style).
Here are some common styles to use:
SWT.ICON_ERRORSWT.ICON_INFORMATIONSWT.ICON_QUESTIONSWT.ICON_WARNINGSWT.ICON_WORKINGSWT.OKSWT.OK | SWT.CANCELSWT.YES | SWT.NOSWT.YES | SWT.NO | SWT.CANCELSWT.RETRY | SWT.CANCELSWT.ABORT | SWT.RETRY | SWT.IGNORE.
You use the message box’s open
method to display it; when the message box is closed, compare this
method’s int return value to one
of the preceding styles such as SWT.YES,
SWT.NO, or SWT.CANCEL to
determine which button was clicked.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access