Java basics-SWT/jface

Source: Internet
Author: User

Java basics --- SWT/jface

I haven't been writing something on my blog for a long time. I 've been studying SWT/jface for this period of time. Didn't I mention it last week? I have the opportunity to study SWT and jface. After more than a week of exploration, I finally changed the address book program designed with swing to the SWT and jface versions. To tell the truth, this process is not so easy. I think you know that it is beyond the two GUI APIs of AWT and swing, there is also a set of Apis specifically designed for designing GUI programs developed by IBM, which is SWT. Standard Widget Toolkit (Standard Widget tool ). So far, Sun still does not accept SWT/jface as a graphical API standard in Java, but it still develops with the help of Eclipse's outstanding performance in an unstoppable manner. The GUI program designed with SWT is completely localized. That is to say, the appearance of the program it designed will change with the changes in the operating system, and it will not remain the same as swing. Efficient performance is also one of the advantages of SWT. Currently, Eclipse, which is popular all over the world, is built based on SWT/jface. Let's take a look at the current eclipse3.2 to see how great it is. There is also little information about SWT/jface in China. If the English is good, you can still find a lot of information on the eclipse official website. When using SWT/jface, I mainly referred to <eclipse from entry to entry> by Mr Chen Gang. This book has helped me a lot, I am very grateful to the author of this book. There is also the eclipse help documentation, and the official website, where you can find a lot of information.

About SWT and jface don't too much introduction, if you are interested, you can go to the http://blog.csdn.net/jayliu this blog to have a look, this predecessors very comprehensive introduction of SWT/jface installation and use. I will summarize some experiences and experiences in designing this address book. The following uses the design of this address book as the main line throughout the full text, step by step to unveil the secret of SWT/jface.

Before that, it is necessary to explain the differences between SWT and jface. before writing it here, I have always written SWT and jface together, which may make people feel that they are the same thing. In fact, they have a lot of difference. When they initially only had SWT APIs, the program designed was too complicated and the code volume was large, in addition, because some APIs used by SWT are relatively low-level and do not comply with the object-oriented approach, the eclipse development team has created a set of SWT interfaces that are easier to use, the more powerful graph package "jface", let's first look at how the official company defines the difference between the two: "The line between SWT and jface is obvious. SWT does not rely on any jface or platform code at all. Jface is used to provide common application UI functions at the top of the SWT library. Jface does not try to "hide" SWT or replace it. It provides classes and interfaces to process many common tasks associated with dynamic UI programming using SWT ." We can understand that jface is only used as part of the SWT function extension, but not all. jface does not replace any function in SWT, instead of using jface, you can use SWT to develop any programs you want. However, for ease of use and other considerations, it is best not to use SWT for components that can use jface during GUI Design. The SWT component is used only when jface cannot meet our needs. Therefore, the SWT program we designed is a mix of SWT and jface, this is why I write SWT/jface together.

The following is a program made with swing and SWT/jface respectively. We can compare the differences between them.

Swing Interface

SWT/jface Interface

1. Create a main interface window
The first thing we need to do is to create a window on the main interface. Here I use the jface applicationwindow class. This class inherits the jface window class and can be added to the menu bar and toolbar. First, let's take a look at the general framework of the window.

Public class mainframe extends applicationwindow {

Public mainframe (){
// Call the constructor of the applicationwindow class. This line must be written. The default constructor of applicationwindow requires shell parameters.
//, We can set this parameter to null.
Super (null );
// Add the menu bar
Addmenubar ();

}
// Because there is a horizontal line under the default menu bar, jface is different from SWT, And the menu bar created by SWT has no horizontal line by default.
. We will rewrite this method to remove this horizontal line.
Protected Boolean showtopseperator (){
.
.
.
Return false;
}
// This method focuses on applicationwindow and is used to add components to the window. Components such as tables are also here
// Added.
Protected control createcontents (composite PARE ){
.
.
.
Return pare;
}
// This method adds the menu bar. jface creates the menu bar by creating menumanager and Action event classes.
// Introduction.
Protected menumanager createmenumanager (){
.
.
.
Return NULL;
}
// The following is the main entry point of the program. This is a step that must be completed before jface creates a window class. The basic mode is the four statements of code.
Public static void main (string [] ARGs ){
// Create an object for this window class
Mainframe jface = new mainframe ();
// Events in the loop listener window
Jface. setblockonopen (true );
// Create a window
Jface. open ();
// Release the resource when the window is closed.
Display. getdefault (). Dispose ();
}
}

This is the basic framework for creating a jface window. If you want to add a toolbar, add another
Protected coolbarmanager createcoolbarmanager (INT style) {} This method writes the tool bar code here, and adds addtoolbar to the constructor (here you can set some styles in the tool bar );

After running the above program directly, a blank window will be created. Does it really feel like a window designed in Java. This is also one of the characteristics of SWT. You can also set properties such as the title and size of the window.

// Obtain the shell object of the window
Shell shell = This. getshell ();
Toolkit kit = toolkit. getdefatooltoolkit ();
// Set the window title
Shell. settext ("my address book-Authorization: Administrator ");
// Set the default window size
Shell. setsize (700,400 );
// Center the window
Shell. setlocation (kit. getscreensize (). Width-700)/2, (kit. getscreensize (). Height-400)/2 );
// Set the minimum window size. This setting is mainly used to change the window size, but does not want to make the window too small to make the window components
// Deformation. Then we can set a minimum range.
Shell. setminimumsize (700,400 );

Add the code above to the createcontents () method.
If you want to set window properties, such as creating a modal window or creating a window with only the close button but not the maximize button, you can add the following to the constructor of the window class:
This. setshellstyle (you can set any style here); The following styles can be set: (you can select multiple attributes. However, if some attributes conflict with each other, other attributes cannot be set)
SWT. Border // create a window with a border but no title bar
SWT. Close // create a window with only the close button
SWT. Min // create a window that cannot be maximized
SWT. Max, // create a window to maximize and minimize
SWT. no_trim // create a window without any boundaries and title bars
SWT. Resize // create a window that can change the size
SWT. Title // create a window without the title bar icon or the close button
SWT. on_top // create a window always on the top. Note: This attribute is best used with close, Min, and Max.
SWT. Tool // create a window similar to a toolbar
SWT. application_modal // create an application modal window
SWT. modeless // create an unmodal window
SWT. primary_modal // create a primary modal window
SWT. system_modal // create a system modal window
There are also two shortcut properties to create a window
Shell_trim // create a standard window, which is equivalent to setting the property to close | title | min | max | resize
Dialog_trim // create a dialog box, which is equivalent to setting the attribute to title | close | border
When multiple options are selected, only select the middle and use | to separate them.

Next we will introduce how to create a menu bar. Using jface to create a menu is not as straightforward as using SWT or swing. However, the process menu events are compared. For example, if we want to process the same event in the menu bar, toolbar, and pop-up menu, we need to write three identical codes in SWT or swing, of course, you can also create an external class to process them in a centralized manner, but it is not convenient to use the external class for names, images and some other information. Jface provides a solution to the preceding problems. jface provides an action class that stores names, images, and action handlers, in this way, you can share these actions to form menu items and toolbar buttons. Of course, menuitem is still used at the bottom layer. It is done by menumanager to convert action to menuitem. Menumanager simplifies menu creation. Once a menumanager object is generated, it can be used in the menu bar, pop-up menu, and toolbar drop-down menu. Next, we will create a menu item with three options: open, save, and exit. And respond to corresponding events. By the way, we will introduce the use of the open and save dialog box.

First, add the code in the createmenumanager () method of the main window:

Protected menumanager createmenumanager (){
// Create a menu manager equivalent to menubar. You can add the menus created later.
Menumanager menubar = new menumanager ();
// Create the first menu item, which is equivalent to the menu in swing. Add the & Symbol in front of the letter to indicate that the shortcut key is followed by the word
// Mother.
Menumanager filemenu = new menumanager ("file (& F )");
// Below is a sub-menu item for our menu, which is equivalent to menuitem
Filemenu. Add (New openfile ());
Filemenu. Add (New SaveFile ());
// The following code inserts a horizontal line
Filemenu. Add (new separator ());
Filemenu. Add (new exit ());
// Add the filemenu menu to menubar.
Menubar. Add (filemenu );
// Return to our menubar menu manager, which must be provided; otherwise, the menu is invisible.
Return menubar;
}

Next we will create several classes in this class that inherit the action. Here I use the internal class method, and of course I can also use the external class.

Private class openfile extends action {
Public openfile (){
// Add @ Ctrl + O to indicate that the single-item accelerator key of this dish is Ctrl + O. Is this easy to set? Do you remember to set the accelerator key in swing or
// Troublesome.
Settext ("open (& O) @ Ctrl + O ");
}
Public void run (){
// Create an object in the filedialog dialog box. The first parameter of its constructor is the shell object of its parent window, SWT.
// Open indicates that this window is a window for opening files
Filedialog = new filedialog (getshell (), SWT. Open );
// Specify the default path when the window is opened. You can set the absolute and relative paths. Here, the path of the current file is set.
Filedialog. setfilterpath ("./");
// Set the name of the filter file. The parameter is a string array. The filter name can be specified or not specified. For example, only
// The names of the two filtered files and the names of the last EXE files
It is not specified, but after a file is set, the file named .exe is used as the name of the filter file.
Filedialog. setfilternames (New String [] {"all files (*. *)", "Microsoft Excel (*. xls )"});
// Set the type of the filter file, which is also a string array. That is to say, it doesn't matter how the above name is set, but what actually filters out files is
// The type specified below.
Filedialog. setfilterextensions (New String [] {"*. *", "*. xls", "*. EXE "});
// Use the open () method to open the dialog box. When the dialog box is closed, an absolute path of the selected file is returned. If you click Cancel
// Or if the dialog box is closed directly, null is returned.
System. Out. println (filedialog. open ());
}
}
// The save file dialog box is basically the same as the open file dialog box. The only difference is that the 2nd parameters in the filedialog constructor are changed to SWT. SA.
// Ve indicates that this dialog box is the dialog box for saving files.
// Is it the same as the Windows file selection dialog box.
Private class SaveFile extends action {
Public SaveFile (){
Settext ("Save (& S) @ Ctrl + S ");
}
Public void run (){
Filedialog = new filedialog (getshell (), SWT. Save );
Filedialog. setfilterpath ("./");
Filedialog. setfilternames (New String [] {"all files (*. *)", "Microsoft Excel (*. xls )"});
Filedialog. setfilterextensions (New String [] {"*. *", "*. xls", "*. EXE "});
System. Out. println (filedialog. open ());
}
}
// Exit, obtain the shell object of the main window, and release it using the dispose () method. The display will also be released at the end of the program.
// Release it.
Private class exit extends action {
Public exit (){
Settext ("exit (& X) @ Ctrl + X ");
}
Public void run (){
Getshell (). Dispose ();
}
}

It seems complicated to create the menu above. However, if we want to create a right-click menu, there are two items in it, which correspond to the opening and saving menu items respectively, at this time, the convenience of jface is shown, because now our window only has no components yet. Now I want to click any position in the blank window to bring up the drop-down menu, add the following lines of code to createcontents () on the main menu ().

// Create a menu manager with the right-click menu
Menumanager popmenu = new menumanager ();
// Reuse the openfile and SaveFile classes we just created
Popmenu. Add (New openfile ());
Popmenu. Add (New SaveFile ());
// Create a menu object. Because there is nothing in the current window, the PARE composite container can receive our mouse clicks.
. Of course, if you want to bring up a menu on another component or container, you only need to change the PARE in the following two statements of code to the corresponding component.
Menu = popmenu. createcontextmenu (PARE );
// Add the menu to the PARE container.
Pare. setmenu (menu );
We can see from the above that as long as an action object is created, we can reuse it in the toolbar or menu bar. This makes it much easier.

It's very late. I will summarize it today and continue next time.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zxy6173/archive/2007/03/04/1520113.aspx

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.