Common SWT components in eclipse development classic tutorial

Source: Internet
Author: User

Before introducing components, it is necessary to introduce the control class. The control class is an abstract class. It is the base class of all window components (that is, the components that can obtain the handle in Windows.

Control class inheritance

The control class is designed for inheritance. All window components inherit from the control class, as shown in 1.

Figure 1 inheritance relationship of control class

An instance of control represents a window component in windows. It has the window name and handle attribute, but cannot be accessed directly in the program.

Common Control Methods

The control class provides common methods in window components. All window components can call the control class methods. The common methods are as follows.

1. setbounds (int x, int y, int width, int height)

Description: sets the position of the window component. The parameter (X, Y) is the coordinate of the vertex in the upper left corner of the window component relative to the parent window. (width, height) is the width and height of the window.

Example: button. setbounds (40, 50,100, 30 ).

2. setenabled (Boolean enabled)

Description: sets whether the window is available. If the parameter enabled is true, the window is available. If the parameter is false, the window is disabled.

Example: button. setenabled (false ).

3. setvisible (Boolean visible)

Description: sets whether the window can be displayed. If the parameter is set to true, the window can be displayed. If it is set to false, the window cannot be displayed.

Example: button. setvisible (false ).

4. settooltiptext (string)

Explanation: sets the prompt information when the cursor points to the window. The string parameter is the content of the prompt information.

Example: button. settooltiptext ("very good ").

5. setfont (Font font)

Description: Set the font of the window text. The font parameter is the font object.

Example: button. setfont (font ).

6. setforeground (color)

Description: Set the foreground color of the window. The color parameter is a color object.

Example: button. setforeground (color ).

7. setbackground (color)

Description: sets the background color of the window. The color parameter is a color object.

Example: button. setbackground (color ).

8. setcursor (cursor)

Description: Set the cursor shape of the window. The cursor parameter is the cursor object.

Example: button. setcursor (new cursor (null, SWT. cursor_wait ));

9. Control (composite parent, int style)

The constructor in the window component generally calls the constructor of the control class. The parent parameter is the parent window of the currently constructed window, style is the style of the current build window (SWT can be specified by default. none ).

Example: button = new button (shell, SWT. None ).

In addition, the control class implements some window-related methods, such as createwidget and createhandle. These methods are directly related to the operating system. Interested readers can continue to study them.

Tip: not all components call the control method. Some methods are stored for some special components.

Common components

Some components exist in most operating systems, and SWT directly encapsulates these components through JNI.

Button

A button is commonly used by SWT components. It is easy to add a button to a component. You only need to specify the parent component of the button and the corresponding style, for example, "button = new button (shell, SWT. "Push)" statement adds a common button to the shell component.

In addition, adding a button usually specifies the button location (if layout information is not specified) and the button display label, as shown in Example 1.

Routine 1 helloworldbutton. Java

/**
* To save space, all import classes have been commented out.
* You can use the CTRL + Shift + O shortcut to automatically introduce the dependent classes.
* Send an email to the ganshm@gmail.com if there is a problem **/
Public class helloworldbutton {
Public helloworldbutton (){
Display display = New Display ();
Shell shell = new shell (Display );
// Specify the parent component and button style
Button button = new button (shell, SWT. Push );
// Specify the button position
Button. setbounds (40, 50,100, 30 );
// Display tag of the specified button
Button. settext ("Click me ");
Shell. setsize (200,200 );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
Display. Dispose ();
}
Public static void main (string [] ARGs ){
New helloworldbutton ();
}
}

The preceding example shows how to add a button in a window. The program running effect is 2.

Figure 2 button component

There are many types of Button styles. In SWT, checkbox and radiobox are different style buttons.

Tip: If the button is a check box or a single button, you can use the "getselection" method to determine whether the button is selected.

Tag

Lable is a common component of SWT. Adding a label to a component is easy. You only need to specify the parent component of the button and the corresponding style, for example, "label Label = new label (shell, SWT. separator | SWT. vertical) "statement adds a label to the shell component.

You can specify a composite style for the SWT component, and the SWT will display the component according to the composite style, as shown in the label example, as shown in example 2.

Routine 2 helloworldlabel. Java
Public class helloworldlabel {
Public static void main (string [] ARGs ){
Display display = New Display ();
Shell shell = new shell (Display );
Shell. setlayout (New filllayout ());
Label label1 = new label (shell, SWT. Wrap );
Label1.settext ("very good! ");
New label (shell, SWT. Separator | SWT. Horizontal );
New label (shell, SWT. Separator | SWT. Vertical );
Label label2 = new label (shell, SWT. None );
Label2.settext ("very good! ");
Shell. setsize (200, 70 );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ())
Display. Sleep ();
}
Display. Dispose ();
}
}

In the preceding example window, four labels are added and different display styles are set for each label. The program running effect is shown in 3.

Figure 3 tag Components

A tag can be used as a component to display text or as a separator. If it is used as a separator, the tag does not display text information.

Text Box

The text component is a text box in SWT. It is easy to add a text box to the component. You only need to specify the parent group of the text box and the corresponding style, for example, "text T = new text (shell, SWT. multi | SWT. border | SWT. wrap | SWT. v_scroll) "statement adds a text box to the shell component.

The text box has multiple display styles, as shown in Example 3.

Example 3 helloworldtext. Java
Public class helloworldtext {
Public static void main (string [] ARGs ){
Display display = New Display ();
Shell shell = new shell (Display );
Shell. setlayout (New gridlayout (1, false ));
// Add a single row text box
New text (shell, SWT. Border );
// Add the right-aligned single-row text box
New text (shell, SWT. Right | SWT. Border );
// Add a text box displayed as a password
New text (shell, SWT. Password | SWT. Border );
// Add a read-only text box
New text (shell, SWT. read_only | SWT. Border). settext ("Read Only ");
// Add a multi-line text box
Text T = new text (shell, SWT. multi | SWT. Border | SWT. Wrap | SWT. v_scroll );
// Assign a value to the text attribute
T. settext ("very long sting test! Very long sting test! Very long sting test! "
+ "Very long sting test! ");
T. setlayoutdata (New griddata (griddata. fill_both ));
Shell. setsize (200,200 );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
Display. Dispose ();
}
}

Five text boxes of different styles are added to the window, and different display styles are set for each text box, as shown in program running effect 4.

Figure 4 text box component

For different types of labels, you only need to specify different styles. As shown above, the text box contains left-aligned, right-aligned, password-box, read-only text box, and multi-line display text boxes.

Drop-down list

The combo component is the drop-down list box in SWT. You can use "combo = new combo (shell, SWT. drop_down) "add a drop-down list box on the shell component. In addition, you can use" combo. setitems (items) sets the drop-down list of the drop-down list box, where "items" is a string array. The drop-down list box has several display styles, as shown in Example 4.

Example 4 helloworldcombo. Java
Public class helloworldcombo {
// Drop-down list items
Private Static final string [] items = {"Alpha", "bravo", "Charlie", "Delta ",
"Echo", "Foxtrot", "golf", "hotel", "India", "Juliet", "kilo", "Lima", "Mike ",
"November", "Oscar", "Papa", "Quebec", "Romeo", "siider", "Tango", "Uniform ",
"Victor", "Whiskey", "x-ray", "Yankee", "Zulu"
};
Public static void main (string [] ARGs ){
Display display = New Display ();
Shell shell = new shell (Display );
Shell. setlayout (New rowlayout ());
// Add the drop-down list box of the drop-down button style
Combo combo = new combo (shell, SWT. drop_down );
// Set the drop-down list items
Combo. setitems (items );
// Add a read-only drop-down list box
Combo readonly = new combo (shell, SWT. drop_down | SWT. read_only );
// Set the drop-down list items
Readonly. setitems (items );
// Add a drop-down list box without a drop-down button style
Combo simple = new combo (shell, SWT. Simple );
// Set the drop-down list items
Simple. setitems (items );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
Display. Dispose ();
}
}

Three drop-down lists with different styles are added to the window, and the display style of the drop-down list box is set. The program running effect is 5.

Figure 5 drop-down list box component

In the preceding example, only the display information of combo is added. Generally, you want the selected item to be associated with an object. After an item is selected, you can directly obtain the selected objects from this item and then operate on these objects. You can use the "Public void setdata (string key, object Value)" and "public object getdata (string key)" methods of the widget class to implement this function.

Combo is a subclass of Widgets. When combo is initialized, you can use the setdata method to associate the string of the item with the corresponding object, when this option is selected, the object of the selected item is retrieved using the getdata method. The widget class saves the reference of the object set by the user through an object array.

Tip: the widget is the parent class of all window components. If the widget supports multiple data display, you can select the data to obtain the objects associated with the widget through the getdata and setdata methods.

List

The list component is a list box in SWT. You can use "list single = new list (shell, SWT. border | SWT. single | SWT. v_scroll) "to add a list box on the shell component. You can also use" setitems (items) "to set the drop-down list of the drop-down list, where" items "is a string array. The list box contains several display styles, as shown in routine 5.

Routine 5 helloworldlist. Java
Public class helloworldlist {
// List items
Private Static final string [] items = {"Alpha", "bravo", "Charlie ",
"Delta", "Echo", "Foxtrot", "golf", "hotel", "India", "Juliet", "kilo ",
"Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo ",
"Siider", "Tango", "Uniform", "Victor", "Whiskey", "x-ray", "Yankee ",
"Zulu"
};
Public static void main (string [] ARGs ){
Display display = New Display ();
Shell shell = new shell (Display );
Shell. setlayout (New filllayout ());
// Add a list box that can only be selected
List single = new list (shell, SWT. Border | SWT. Single | SWT. v_scroll );
// Add a list item
For (INT I = 0, n = items. length; I <n; I ++ ){
Single. Add (items [I]);
}
// Select 5th items
Single. Select (4 );
// Add a list box that can be selected multiple times
List multi = new list (shell, SWT. Border | SWT. multi | SWT. v_scroll );
// Add a list item
Multi. setitems (items );
// Select items 10th to 12th
Multi. Select (9, 11 );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
Display. Dispose ();
}
}

In the preceding example window, two lists are added. One is a single-choice list box and the other is a multiple-choice list box. The program running effect is shown in Figure 6.

Figure 6 list box Components

Tip: Select multiple values in the list box, which can be consecutive or discontinuous list items. If you select a nonconsecutive list item, hold down the Shift key and select it with the mouse.

Component attributes

When creating a component, you should specify the attributes of the component. The attributes of the component include the style and alignment of the component. The following describes the attributes of the button component, the attributes of other components are similar.

Component Style

You can use "org. Eclipse. SWT. Widgets. Button" to create a "button" for SWT. The new button can specify parameters of different styles as follows.

· SWT. Push: Push Button (Common button ).

· SWT. Check: check box button.

· SWT. Radio: Single-choice button.

· SWT. Toggle: The toggle button (normal button with status ).

· SWT. ARROW: arrow button.

· SWT. Flat: Flat button.

· SWT. Border: A button with a border.

SWT. Flat, SWT. border, and other styles can coexist. The button style is shown in table 1.

Alignment of components

You can set the alignment for the text in the button. In SWT, there are three button Alignment Methods: Left alignment, right alignment, and center. In addition, when the arrow button is used, you can set the arrow up or down, as shown in table 2.

In addition, you can not only set the button style, alignment, and status, but also set the button image using the setimage method. In addition, you can combine these styles, Alignment Methods, and States to make the buttons meet your needs, such as the style "SWT. border | SWT. "Radio" can be set as a single button with a border.

Related Article

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.