SWT dialog box int style = SWT. application_modal | SWT. Yes | SWT. No;
MessageBox = new MessageBox (shell, style );
MessageBox. settext ("information ");
MessageBox. setmessage ("Close the shell? ");
Event. doit = MessageBox. open () = SWT. Yes;
16:03:06 | classification: Java
| Label: | large font size, small/medium subscription
For the dialog box, I want to introduce MessageBox (Org. Eclipse. SWT. Widgets. MessageBox) in SWT ). In fact, if you have read this series of articles about the SWT event mode, you may have noticed that I used MessageBox.
The MessageBox in SWT allows us to change the appearance of the dialog box by specifying the style. For example, if the style of the dialog box contains SWT. OK, it will have a confirmation button; if it contains SWT. cancel, there will be a Cancel button, if it contains icon_question, then the pop-up dialog box will have a question mark icon, and so on.
For example, let's look at the following program:
MessageBox dialog = new
MessageBox (shell, SWT. OK | SWT. icon_information );
Dialog. settext ("hello ");
Dialog. setmessage ("Hello, world! ");
Dialog. open ();
Code snippet 13
Here we can see from the style parameter of the constructor that this is a dialog box with an information icon (an exclamation point) and a confirmation button. See the following figure for details:
Figure 14
Comparison: messagedialog in jface
In the previous section, I briefly introduced MessageBox in SWT. In jface, we use messagedialog (Org. Eclipse. jface. dialogs. messagedialog) to implement similar functions.
To implement a dialog box with information prompts and confirmation buttons as shown above, we only need to write it like this:
Messagedialog. openinformation (shell, "hello", "Hello, world ");
After running, the dialog box size may be different from that in SWT. But is it convenient? In fact, everything jface can do through SWT programming, but generally, jface simplifies programming.
In messagedialog, there are several static methods similar to openxxxx, such:
Openwarning: open a dialog box with a warning icon
Openconfirm: open a dialog box with only the OK and cancel buttons
...
However, you may have noticed that the return types of these static methods are different, some are void and some are Boolean. These return values (if any) reflect the button selected by the user. For example, openconfirm. If you press OK, true is returned.
Input box: inputdialog
Relatively speaking, the usage of the input dialog box may be more responsible. You must inputdialog object and then call its open method to open the dialog box, obtain the return value of this method (to determine whether the user clicked OK or canceled ). Then, use the getvalue () method to obtain user input.
For better image, let's give an example. We still use the preceding Hello, world! Program, but change its createcontents Method to the following code:
1 @ override
2 protected control createcontents (final composite parent ){
3 button = new button (parent, SWT. None );
4 button. settext ("Click me! ");
5 button. addselectionlistener (New selectionadapter (){
6 @ override
7 publicvoid
Widgetselected (selectionevent arg0 ){
8
9 inputdialog input =
New inputdialog (parent. getshell (),
10 "input dialog title", "Please input some string here :",
11 "Initial Value", null );
12 if (input. open () = Window. OK)
13 {
14 system. Out. println (input. getvalue ());
15}
16}
17
18 });
19 Return parent;
20}
21
Code segment 14
After running, the interface is shown in the figure below:
Figure 15
If you press OK, the console displays the information you entered.
SWT has different types of dialog boxes. Some dialogs have special properties.
MessageBox =
New MessageBox (shell, SWT. OK | SWT. Cancel );
If (MessageBox. open () = SWT. OK)
{
System. Out. println ("OK is pressed .");
}
The open () method of each dialog box returns different types. For example, the MessageBox dialog box returns the int type from the open () method. Therefore, you need to write different conditions to process the return values of each dialog box.
MessageBox is used to give feedback to users. You can use or (|) to combine different styles, as shown in source code 3:
Source code 3. Example of MessageBox
MessageBox =
New MessageBox (shell,
SWT. OK |
SWT. Cancel |
SWT. icon_warning );
MessageBox. setmessage ("www.korayguclu.de ");
MessageBox. open ();
The available button constants are listed below. Enable or perform the Union of Different buttons. Create a dialog box for the SWT framework according to style bits. Button constants include: SWT. Abort, SWT. OK, SWT. Cancel, SWT. Retry, SWT. Ignore, SWT. Yes, and SWT. No.
Available icons include:
SWT. icon_error
SWT. icon_information
SWT. icon_question
SWT. icon_warning
SWT. icon_working