SWT/jface dialog on the fourth day

Source: Internet
Author: User

SWT/jface provides a variety of dialog, which can generally meet our needs. Now we will introduce these dialog,
In the next article, we will continue to define the Dialog
1. messagedialog
1. messagedialog is easy to use. You only need one sentence.

Messagedialog. openinfomation (shell, title, message );
Messagedialog. openconfirm (shell, title, message );
Messagedialog. openquestion (shell, title, message );
Messagedialog. openerror (shell, title, message );
2. The four differences are as follows:
Prompt icon, displayed button, Return Value
Openinfomation is similar to the I icon and a fixed void
Openconfirm question mark icon determines and cancels Boolean
Openquestion question mark icon yes and no Boolean
Openerror: the void of an X icon is determined.

Finally, the size of messagedialog cannot be automatically adjusted based on the content, and it is larger when a small amount of information is prompted.
3. The input dialog of inputdialog

Inputdialog = new inputdialog (shell, "title", "prompt information", "Default Value", null );
If (inputdialog. open () = inputdialog. OK ){
String value = dialog. getvalue ();
} Note that the last parameter is "validators". If you want to verify the data, you can provide a validators and use them here. Verify the statement:
Public class myvalidator implements iinputvalidaor {
Public String isvalid (string newtext ){
Float value = 0;
Try {
Value = float. valueof (newtext). floatvalue ();
} Catch (numberformatexception e ){
Return "error: enter a value ";
If (value> 0 & value <100 ){
Return NULL;
} Else {
Return "error: enter a number greater than 0 and less than 100 ";
Then rewrite the definition statement
Inputdialog = new inputdialog (shell, "title", "prompt information", "Default Value", new myvalidator (); 2. messagedialog cannot be adjusted automatically, so what can be automatically adjusted? The answer is MessageBox.
Also inherited from dialog.
1. Usage: the use of MessageBox is slightly complicated. Three MessageBox MB = new MessageBox (Shell) statements are required );
MB. setmessage ("prompt message ");
MB. open (); 2, MessageBox can also control the number of icons and buttons
It is implemented by adding a style when new is used, for example
MessageBox MB = new MessageBox (shell, SWT. _ icon_quest | SWT. Yes | SWT. No );
People who have developed SWT should be familiar with this format and it is everywhere.

Icon type:
SWT. icon_error
SWT. icon_information
SWT. icon_question
SWT. icon_warning

Button combination:
SWT. OK | SWT. Cancel
SWT. Yes | SWT. No
SWT. Yes | SWT. No | SWT. Cancel
SWT. Retry | SWT. Cancel
SWT. Abort | SWT. Retry | SWT. Ignore
Let's take a closer look at the various dialogs we usually see.
3. Color Selection dialog box
Not to mention, the usage is almost everywhere in SWT.

Colordialog dialog = new colordialog (Shell );
RGB = dialog. open ();
If (RGB! = NULL ){
Color color = new color (shell. getdisplay (), RGB );
Note that dispose () is required after color is used ();
4. font selection dialog box
With some of the above experiences, you can write with the help of an eclipse editor.
Fontdialog dialog = new fontdialog (Shell );
Fontdata = dialog. open ();
If (fontdate! = NULL ){
Font font = new font (shell. getdisplay (), fontdata );
Also font. Dispose ();
5. Print the dialog box. Use the same method as above. Use dispose as above.
Printdialog dialog = new printdialog (Shell );
Printdata = dialog. open ();
If (printdate! = NULL ){
Printer printer = new printer (printerdata );
Vi. file selection dialog box: Same as above
1. Implementation Method
Filedialog dialog = new filedialog (shell, SWT. Open );
Dialog. setfilterpath ("C: \ Windows"); // you can specify the initial path.
String filename = dialog. open (); // return the full path (path + file name) 2. If you want to select multiple files, add SWT. multi
Filedialog dialog = new filedialog (shell, SWT. Open | SWT. multi );
String filename = dialog. open (); // return the full path of the last selected file
String [] filenames = dialog. getfilenames (); // return all selected file names, excluding paths
String Path = dialog. getfilterpath (); return the selected path. This works with filenames to obtain the full path of all files. 3. Add filter.
Filedialog dialog = new filedialog (shell, SWT. Open | SWT. nulti );
Dialog. setfilternames (New String [] {"executable file (*. EXE)," Excel (*. xls), "All Files ""});
Dialog. setfilterextensions (New String [] {"*. EXE", "*. xls ","*.*"});
// The acceptance method is the same as the method in 2. The difference is that the filter is added. 4. Save dialog box
The Save dialog box does not provide the SAVE Function. You need to implement this function by yourself. It only provides the interface and obtains the selected directory and input file name.
Filedialog dialog = new filedialog (shell, SWT. Save );
String filename = dialog. open ();


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.