Dialog provided by SWT/jface

Source: Internet
Author: User

SWT/jface provides a variety of dialog, which can generally meet our needs. Now we will introduce these dialog,
Next articleArticleContinue to customize 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
Usage:

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. As mentioned above, messagedialog cannot be automatically adjusted. What can be automatically adjusted? The answer is MessageBox.
Also inherited from dialog.
1. Usage: the use of MessageBox is a little complicated and requires three sentences.

MessageBox MB = New MessageBox (Shell );
MB. setmessage (
" Prompt information " );
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. 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 " ); // Set the initial path
String filename = Dialog. open (); // Returned 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 (); // Returns the full path of the last selected file.
String [] filenames = Dialog. getfilenames (); // Returns 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 ();

 

Reprinted from: http://apps.hi.baidu.com/share/detail/15809844

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.