Common SWT Components

Source: Internet
Author: User

1. Button)

(1) Common style of the button component

SWT. Push Button

SWT. Check multiple buttons

SWT. Radio radio button

SWT. arrow button

SWT. None default button

SWT. Center text center and SWT. None

SWT. Left left alignment

SWT. Right

SWT. Border deep button

SWT. flat button

A button can specify multiple styles. You only need to connect the specified styles with the symbol "| ".

Button bt = new button (shell, SWT. Check | SWT. Border | SWT. Left );

Multiple choice, deep, left aligned

(2) common methods for button Components

Settext (string), set the label Text of the component

Setbounds (int x, int y, int width, int height); // you can specify the coordinates and sizes of a widget.

Setenabled (Boolean enabled); Sets whether the component is available. The default value is true.

Setfont (Font font); set the font of Text

Setforeground (color); set the foreground color

Setbackground (color); set the background color

Setimage (image); sets the image used for display

Setselection (Boolean selected); Sets whether to select. The default value is false.

Settooltiptext (string); displays the prompt when you move your cursor over the component.

 

2. Label)

(1) common label patterns

SWT. Center

SWT. Right

SWT. Left left alignment

SWT. None default Style

SWT. Wrap automatic line feed

SWT. Border deep-seated

SWT. separator, which is a vertical bar by default

Horizontal horizontal line column break

 

3 text box component (text)

(1) common text styles

SWT. None default Style

SWT. Center

SWT. Right

SWT. Left

SWT. multi can input multiple lines. You need to press enter to wrap the line.

SWT. Wrap can input multiple lines and wrap automatically

SWT. Password: the password type. The input character is displayed as "*".

SWT. Border deep-seated

SWT. v_scroll vertical scroll bar

SWT. h_scroll horizontal scroll bar

 

4. drop-down box component (Combo)

(1) Common combo styles

SWT. None default

SWT. read_only read-only

You do not need to click the drop-down box for SWT. Simple.

 

(2) common methods of combo drop-down boxes

Add (string) add an item on combo

Add (string, int index) inserts an item after the index (starting from 0) of combo

Deselectall () to empty the current option in the combo component

Removeall () clears all options in combo

Setitems (string [] items) adds the items in the array to combo in sequence.

Select (INT index) sets the index + 1 of combo to the current selected item.

 

5. List component)

(1) list common styles

SWT. None default Style

SWT. v_scroll with vertical scroll bar

SWT. multi allow check

SWT. Single allow single choice

 

(2) Common Methods

The methods of the list and combo components are the same, but since the list can select multiple, Combo can only select one

So the list does not have the gettext () method. The value of list uses getselection () and returns a string

Array.

 

6. menu (menuitem)

Menus (menu and menuitem) are common SWT components. Menus are a menu bar and a container.

, Can accommodate menu items (menuitem)

(1) menu style

SWT. Bar menu bar for Main Menu

SWT. drop_down drop-down menu for sub-menu

Right-click SWT. pop_up and choose menu

(2) menuitem Style

SWT. Cascade menu item with sub menu

After SWT. Check is selected, a small check mark is displayed.

SWT. Push common menu

After SWT. Radio is selected, a dot is displayed.

SWT. Separator

(3) general steps for creating a menu

① Create a menu bar and use the SWT. Bar attribute.

Menu mainmunu = new menu (shell, SWT. bar );

② Specify the menu bar to be displayed in the form

Shell. setmenubar (mainmenu );

③ Use the SWT. Cascade attribute to create a top-level menu item.

Menuitem fileitem = new menuitem (mainmenu, SWT. Cascade );

Fileitem. settext ("file & F ");

④ Create drop-down menus related to top menu items

Menu filemenu = new menu (shell, SWT. drop_down );

⑤ Associate top menu items with drop-down menus

Fileitem. setmenu (filemenu );

Level-2 menu creation only needs to be repeated ③ ~ ⑤

 

Container Type

1 Panel (composite)

(1) common methods of Composite

Getlayout () to obtain the layout manager

Getlayoutdata () to get layout data

Getparent () to get the parent container that holds the container

Getshell () to get the shell containing the container

Layout () re-layout the components on the container, which is equivalent to refreshing

 

 

2. Group)

Group is a subclass of composite, so the usage of the two is basically the same. The main difference is that the group displays a box

The description text can also be displayed on the box.

 

3 tabs (tabfolder, tabitem)

The tab includes a tab (tabfolder class) and an option page (tabitem class). tabfolder is a container and can

To accommodate other containers and components, but tabitem is not a container, You can regard it as an option label, tabfolder through

Tabitem to control the components. Each tabitem uses setcontrol () to control an interface component.

 

Layout manager

1. filllayout)

Filllayout is the simplest layout manager. It fills the entire container in one row or column, and forces the component size to be consistent.

Generally, the component height is the same as that of the highest component, and the width is the same as that of the widest component. Filllayout cannot be broken, and the boundary distance and spacing cannot be set.

If there is only one component in the container, the component will be filled with the whole container.

(1) constructor

Filllayout () creates an object filled with containers by one row.

Filllayout (INT type) creates an object filled with containers of the specified type. The types include: SWT. Horizontal (ROW)

SWT. Vertical (column)

(2) common attributes

Int type specifies the type of the component filled with containers.

Filllayout. type = SWT. Vertical or SWT. horizontal;

 

2-row layout (rowlayout)

Rowlayout can be a component line display, and you can set the boundary distance and spacing. In addition, you can use setlayoutdata () for each component ()

Method To set the rowdata object. Rowdata is used to set the component size.

(1) constructor

Rowlayout () creates an object for placing components by row

Rowlayout (INT type) creates an object for placing components of the specified type. Type: SWT. Horizontal SWT. Vertical

(2) common attributes

The width (in pixels) of the int marginwidth component from the container edge. The default value is 0.

The height (in pixels) between the int marginheight component and the container edge. The default value is 0.

The distance (in pixels) between the int margintop component and the container edge. The default value is 3.

The distance (in pixels) between the int marginbottom component and the bottom edge of the container. The default value is 3.

The distance between int spacing components. The default value is 3.

Boolean justify if this attribute is true, the distance between components increases as the container is stretched. The default value is false.

Boolean wrap if this attribute is true, the row is automatically folded when the space is insufficient. The default value is true.

Boolean Pack: If this attribute is true, the component size is set; if it is false, the component size is forced to be the same

The default value is true.

Int tyep SWT. Horizontal (ROW) SWT. Vertical (column)

(2) rowdata class

Rowdata is called the layout data class of rowlayout. It can be used to change the component appearance and shape in the container. Its construction method is

Rowdata (INT width, int height)

 

3. gridlayout)

Gridlayout is a practical and powerful Standard Layout and a complicated layout. This layout divides containers into grids.

To place components in the grid. Gridlayout has many configurable attributes. Like rowlayout, it also has a dedicated layout data class.

The griddata. gridlayout constructor has no parameters. However, you can set the structure arrangement by using griddata and setting the gridlayout attribute,

Shape, and position.

(1) gridlayout attributes

Int numcolumn sets the number of columns of the container. The component is placed from left to right by column. When the number of components exceeds the number of columns, the next component

Automatically add to a new row

Boolean makecolumnsequalwidth: Forces columns to have the same width. The default value is false.

Int marginwidth sets the horizontal distance between the component and the container edge. The default value is 5.

Int marginheight: sets the vertical height of the component and container edge. The default value is 5.

Int horizontalspacing sets the spacing between columns. The default value is 5.

Int verticalspacing: Specifies the interval between rows. The default value is 5.

 

(2) Layout data class (griddata class)

Griddata is a layout data class dedicated to gridlayout. Using griddata, you can build many complex layout methods.

① Constructor

Griddata () creates an object whose property value is the default value.

Griddata (INT type)

② Common griddata types

Griddata. Fill is usually used with object attributes horizontalalignment and verticalignment, and is full of objects.

Attribute specifies the space.

Griddata. fill_horizontal horizontal full,

Griddata. fill_vertical vertical fill

Griddata. fill_both bidirectional fill

Griddata. horizontal_align_beginning align horizontally

Griddata. horizontal_align_end horizontal align to right

Griddata. horizontal_align_center horizontal center alignment

③ Griddata has the following object attributes:

Int horizontalspan sets the number of columns occupied by the component. The default value is 1.

Int verticalspan sets the number of lines occupied by the component. The default value is 1.

Horizontalalignment sets the component alignment to horizontal

Verticalignment sets the component alignment to vertical.

Grabexcesshorizontalspace to seize extra horizontal space

Grabexcessverticalspace to seize extra vertical space

Horizontalalignment and verticalignment can take the following values:

Geginning (left and top when horizontal alignment is performed)

Center, default

End (right in Horizontal alignment and bottom in vertical alignment)

Fill full

 

4. formlayout)

Formlayout is a flexible and precise layout method. formdata makes it a dedicated layout data class.

In addition, a formattachment class is added. Formattachment defines the four sides of the component and the parent container

(Shell, composite) margin, to ensure that the relative position of the component in the parent container remains unchanged, formattachment

Class uses different constructor methods to locate components. With formdata and formattachment, you can create complex

When the size of the main form changes, the relative position of the component remains unchanged.

(1) formlayout Constructor

Formlayout ();

(2) attributes of formlayout

Int marginwidth // sets the horizontal distance between the component and the container edge. The default value is 0.

Int marginheihgt // sets the vertical distance between the component and the container edge. The default value is 0.

(3) formdata class

① Formdata Constructor

Formdata ()

Formdata (INT width, int height) sets the width and height of the component.

② Formdata attributes

Width: Set the component width.

Height: sets the height of a widget.

Top and formattachment work together to set the margins at the bottom of the component and the bottom of the parent container

Left and formattachment work together to set the border on the right of the component and the right of the parent container

If the width and height of formdata are the same as those of formattachment

If a conflict occurs, it is set according to formattachment. The value of width and height does not work.

(3) formattachment class

Attachment means attaching and pasting. The formattachment class is used to specify that the component is pasted in the parent container.

Location. The formattachment Method for Calculating the component pasting position and component size is based on the following expression:

Y = ax + B

① Formatattachment Construction Method

The formatattachment () component is close to the left and top edges of the parent container. If formlayout is set for the parent container

Marginwidth and marginheight are the upper and left edges of the parent container.

Formatattachment (Control) takes the specified component control as a reference

Formatattachment (Control, int offset) takes the specified component control as a reference object and

The component offset is offset.

Formatattachment (Control, int offset, int alignment) Alignment

SWT. Top SWT. Bottom SWT. Left SWT. Right SWT. Center

Formattachment (int m, int N, int offset) is given

Component positioning. M is a molecule, n is the denominator, and offset is the offset.

Formattachment (int m, int offer) N is 100 by default

Formattachment (INT m) N is 100 by default, and offset is 0 by default.

 

 

Common SWT events

All events, listeners, and adapters are included in the org. Eclipse. SWT. Events package.

(1) Common SWT events are as follows:

① Addmouselistener mouse listener

Mousedown ()

Mouseup ()

Mousedoubleclick ()

② Addkeylistener button listener

Keypressed ()

Keyreleased ()

③ Addselectionlistener component selection listener

Widgetselected ()

④ Addfocuslistener focus listener

Focusgained () gets the focus

Focuslost () loses focus

Common SWT Components

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.