Advanced SWT widegts 2nd

Source: Internet
Author: User

Continue the translation ~ Ah, life ~ Csdn, you and you ~ I'm speechless ...... Why put such a function in a page if it is not saved !??

Trees
Tree structures are often used to display information hierarchically. For example, a file manager displays folders containing subfolders, or displays information about classes and their subclasses in the browser window of class tools. The following code (treedemo. Java) creates a tree structure. Standard styles of the tree structure include SWT. multi, SWT. Single, and SWT. Check. The single style limits the tree structure that only one object (item) can be selected at a time, while multi allows multiple selections ). You can select only one style at a time. Check style adds a check box before each object in the tree structure ). For example, eclipse uses the check-style tree structure to allow users to choose which files to import or export in a folder.
The tree structure we create below allows multiple selections with borders. As usual, the first parameter of the tree structure constructor is a composite, which specifies the container in which the tree is placed. The second parameter is the Style Constant of SWT.

Final TREE tree = new tree (shell, SWT. multi | SWT. Border );
Tree. setsize (150,150 );
Tree. setlocation (5, 5 );

We must add the object (treeitems) to this tree. First, we create three objects at the top (Root) level. The first parameter of the constructor is the tree object to be placed (because treeitem belongs to the tree, just as the control belongs to a composite ). The second parameter is the Style Constant of SWT, And the last optional parameter is the index number of the object in the tree. In this example, parameter 1 indicates that the "My doucuments" object is in the middle of the other two objects, even if it is the last added object.

Treeitem mycomp = new treeitem (tree, SWT. None );
Mycomp. settext ("My Computer ");
Treeitem netplaces = new treeitem (tree, SWT. None );
Netplaces. settext ("My Network places ");
Treeitem mydocs = new treeitem (tree, SWT. None, 1 );
Mydocs. settext ("My Documents ");

Create a self-object and display it under the "my computer" node. We use a similar constructor. We can also use the last optional parameter to specify the index number of the newly added element in the Child tree.

Treeitem harddisk = new treeitem (mycomp, SWT. None );
Harddisk. settext ("Local disk (C :)");
Treeitem floppy = new treeitem (mycomp, SWT. None, 0 );
Floppy. settext ("3.5 floppy (:)");

We can create treetiems to generate more sub-trees. Here we create a "Local disk" object to extend the tree to a three-tier structure.

Treeitem progfiles = new treeitem (harddisk, SWT. None );
Progfiles. settext ("Program Files ");

We can also add images to treeitem. As usual (see the section on images), we load the image and use the setimage method of treeitem to add the image display:

Image icon = new image (display, "arrow.bmp ");
Progfiles. setimage (icon );

When the progfiles object is displayed in the tree, the image is displayed on the left of the text description of the object.
Now we add listening events to the tree structure. In the above menus example, a listener is added to menuitems, but in the tree structure example, the Monitored object is the entire tree! We add a selection listener to the tree, and then we can use the getselection () method of the tree to return a list of selected objects. In the following example, whether the listening tree has changed the selected object, if so, an array set of the selected treeitem is obtained, and the text attribute content of each object is printed in the output window.

Tree. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
Treeitem [] T = tree. getselection ();
System. Out. Print ("selection :");
For (INT I = 0; I <t. length; I ++ ){
System. Out. Print (T [I]. gettext () + ",");
}
System. Out. println ();
}
});

We can also use the treelistener object to listen to events in the tree to collapse or expand internal objects or Subtrees. We can output "tree collaspsed" or "tree expanded" information when a corresponding event occurs.

Tree. addtreelistener (New treelistener (){
Public void treecollapsed (treeevent e ){
System. Out. println ("tree collapsed .");
}
Public void treeexpanded (treeevent e ){
System. Out. println ("tree expanded .");
}
});

The program running result should look a little similar to the image below:


If you use the SWT. Check style of the tree, you can use the getchecked () method of treeitem to check whether a unique object in the tree is selected.

<---------------------------------------------------->-<Soso cute split line>-<--------------------------------- B --------------------->

Ah, I can only say that the blog function of csdn is not user-friendly ...... I accidentally pressed a backspace, and paid for it ...... Silence ...... At Am, I had to go to a company in Xiaoshan for an interview. I was dizzy and checked on the map ~ It's now at AM ~ I have finished reading section 14 of Pb just now ~ I hope to finish it early, so don't get an endless n season ~ I think of my previous experiences with reading X-Files ~ 9 season Ah ~ Tortured me ...... I will not directly edit it here in the future. I will use NotePad to do it all ...... Ah ......

<---------------------------------------------------->-<Soso cute split line>-<--------------------------------- e ------------------->

Toolbars
Now let's take a look at the toolbar controls. The toolbar is a tool that saves users from the trouble of searching in the stacked menu and provides a convenient

Quick and Efficient Shortcuts! It makes frequently used tasks more efficient. In addition to images and text

And the layout is placed in a group, which is exactly the same as the standard button.
We will create a simple toolbar (toolbardemo. Java) consisting of some standard buttons, some with images, and some

Text. First, create a toolbar in shell.

Final toolbar bar = new toolbar (shell, SWT. Horizontal );
Bar. setsize (380,150 );
Bar. setlocation (10, 10 );

We want to install images on some buttons, so we must first load the image into the image object.

Image icon = new image (display, "arrow.bmp ");

Then, we instantiate three toolitems (toolbar objects, just like the relationship between tree and treeitem mentioned above ). Every

Basic button style. The first button contains "push" text, the second will contain an image we just loaded, and the last one is

The combination of the first two -- buttons that contain images and text.

Toolitem pushitem1 = new toolitem (Bar, SWT. Push );
Pushitem1.settext ("push ");
Toolitem pushitem2 = new toolitem (Bar, SWT. Push );
Pushitem2.setimage (icon );
Toolitem pushitem3 = new toolitem (Bar, SWT. Push );
Pushitem3.settext ("push ");
Pushitem3.setimage (icon );

Just like in the menu, we can use the separator style to create a visible separation line between different objects in the toolbar.

Toolitem Sep = new toolitem (Bar, SWT. separator );

Similarly, similar to normal buttons and menu controlsCheckAndRadioStyle button. Toolitem

The getselection () method can be used to determine which single button or check box button is selected.

Toolitem checkitem = new toolitem (Bar, SWT. Check );
Checkitem. settext ("check ");
Toolitem sep2 = new toolitem (Bar, SWT. separator );

Now we will add two single-choice buttons, one separator line and the other two single-choice buttons. There are two more important things to remind everyone. First

The status of the single-choice button is automatically set (relative to the menu, the status must be manually controlled by the programmer ).

An adjacent single-choice button will apply the same behavior status. If another toolitem object is placed between two single-choice objects

Then, the two separated single-choice buttons will not be grouped into a group (as we all know, single-choice buttons are only useful After grouping ),

They can be selected independently at the same time. The following code demonstrates this situation: a separator object puts two adjacent radio

It can be divided into two groups ".

Toolitem radioitem1 = new toolitem (Bar, SWT. Radio );
Radioitem1.settext ("Radio 1 ");
Toolitem radioitem2 = new toolitem (Bar, SWT. Radio );
Radioitem2.settext ("Radio 2 ");
Toolitem sep3 = new toolitem (Bar, SWT. separator );
Toolitem radioitem3 = new toolitem (Bar, SWT. Radio );
Radioitem3.settext ("Radio 3 ");
Toolitem radioitem4 = new toolitem (Bar, SWT. Radio );
Radioitem4.settext ("Radio 4 ");

The last tool bar contains a drop-down button. The drop-down button on the left half is like a standard push button, while the right half has

The drop-down arrow masks a drop-down menu. To attach a drop-down menu to an object in the drop-down toolbar, you must obtain

Select events on this object, and use selectionevent (x, y) to determine where to place the corresponding pop-up menu on the screen. In

On the SWT Resources page of eclipse.org, a technical code snippet is displayed. Let's take it and modify it to suit me.

Examples.

Final toolitem dropdown = new toolitem (Bar, SWT. drop_down );
Dropdown. settext ("drop-down ");
Final menu = new menu (shell, SWT. pop_up );
Menuitem choice = new menuitem (menu, SWT. Push );
Choice. settext ("Choices ");
Dropdown. addlistener (SWT. Selection, new listener (){
Public void handleevent (event ){
If (event. Detail = SWT. Arrow ){
Rectangle rect = dropdown. getbounds ();
Point Pt = new point (rect. X, rect. Y + rect. Height );
PT = bar. todisplay (PT );
Menu. setlocation (Pt. X, Pt. y );
Menu. setvisible (true );
}
}
});

 

Finally, we will demonstrate some basic event processing in the toolbar. For example, add a selectionlistener listener to a push button.

. When the correct (leftmost) button is pressed, a message is output on the console.

Pushitem1.addselectionlistener (New selectionlistener (){
Public void widgetselected (selectionevent e ){
System. Out. println ("Push Button one selected .");
}
Public void widgetdefaselecselected (selectionevent e ){
}
});

 

The result should look similar to the following texture:

Coolbars
Coolbar is used to make the created toolbar look similar to a single object, and these individual objects can be dynamic by users.

You have to relocate the tool (just like the tool bars in many software are a group and can be changed, of course, in groups ). Example

Example: on the screen, there are some coolbars at the top of the eclipse work platform. You can click these vertical bars (it looks like this)

:) Drag the button set and locate it in the toolbar. Similar coolbar can also be seen on Microsoft's word and IE

Trace.
We will create a simple coolbar (coolbardemo. Java) example, which includes three internal objects (coolitem ). Each

A coolitem represents a coolbar that can be moved. We can place any controls we want on each coolitem. In

In the following example, we will create an empty coolitem, two coolitems, some buttons respectively, and

Coolitem.
We create a coolbar according to the standard creation control method and set its border style. Create four coolitems and add them

In this bar.

Final coolbar bar = new coolbar (shell, SWT. Border );
Coolitem Item1 = new coolitem (Bar, SWT. None );
Coolitem item2 = new coolitem (Bar, SWT. None );
Coolitem item3 = new coolitem (Bar, SWT. None );
Coolitem item4 = new coolitem (Bar, SWT. None, 2 );

Next, we must create each control on the coolitem. The first is a border and flat button, which is used to hide the lock and unlock

Lock the coolbar.

Button button1 = new button (Bar, SWT. Flat | SWT. Border );
Button1.settext ("button ");
Button1.pack ();

Use the pack method to set the correct size. Next, we add a listener, which is called whenever this button is clicked.

Use bar. setlocked (Boolean) to lock or unlock the coolbar.

Button1.addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
Bar. setlocked (! Bar. getlocked ());
}
});

The next button is a normal button.

Button button2 = new button (Bar, SWT. Push );
Button2.settext ("another button ");
Button2.pack ();

The most common function of coolbar is to create a toolbar where users can freely arrange their positions. This is also used by eclipse. Next Generation

Two basic buttons are placed on the toolbar of the coolitem.

Toolbar tools = new toolbar (Bar, SWT. None );
Toolitem b1 = new toolitem (tools, SWT. Flat );
B1.settext ("tool ");
Toolitem b2 = new toolitem (tools, SWT. Flat );
B2.settext ("bar ");
Tools. Pack ();

After each control is set, we can use the setcontrol () method of coolitem to add the control to coolitem. We can also

Use the setsize method to set the initial size of each control. Note that the setminimumsize method called by item3 ensures that

When you adjust the coolbar, the size of the coolitem containing the toolbar will not shrink below the size we set -- the minimum size here is set

The size of the toolbar.

Point size = button1.getsize ();
Item1.setcontrol (button1 );
Item1.setsize (item1.computesize (size. X, size. y ));
Size = button2.getsize ();
Item2.setcontrol (button2 );
Item2.setsize (item2.computesize (size. X, size. y ));
Size = tools. getsize ();
Item3.setcontrol (tools );
Item3.setsize (item3.computesize (size. X, size. y ));
Item3.setminimumsize (size );

Finally, the setwrapindices method is used to display the new line of the controller when a specific index number is used. The setsize method of the bar instance is displayed on the shell.

It indicates the size of the image.

Bar. setwrapindices (New int [] {3 });
Bar. setsize (300,120 );

Here is a coolbar initialization display:

Drag and re-rank and lock the coolbar:

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.