GridLayout layout _java Programming of SWT (JFace) Experience

Source: Internet
Author: User
Tags pack sleep
GridLayout Layout

The function of GridLayout layout is very powerful, it is also a kind of layout method commonly used by the author. GridLayout is a grid layout that divides the parent component into a table, by default each subassembly occupies a cell's space, and each subassembly is arranged in the table in the order that it is added to the parent component. GridLayout provides a number of properties that allow you to flexibly set the grid information. In addition, the GridLayout layout provides a Griddata class in which subcomponents can set up corresponding griddata, such as "Dogphoto.setlayoutdata (Griddata)", and Griddata can set information for each component as a cell.

the style of GridLayout

The GridLayout class provides information for dividing the grid in the GridLayout layout, and is set mainly through the following several parameters.

NumColumns: The "Gridlayout.numcolumns" property allows you to set up a number of columns in the parent component to display the subassembly.

Makecolumnsequalwidth: Through "gridLayout." The Makecolumnsequalwidth property can set whether the parent component's neutron component has the same column width, and when Makecolumnsequalwidth is true, each column has an equal column width.
MarginLeft: Represents the number of pixel points in the current component from the left margin of the parent component.
MarginRight: Represents the number of pixel points in the current component from the right margin of the parent component.
MarginTop: Represents the number of pixels of the current component from the top margin of the parent component.
MarginBottom: Represents the number of pixel points in the current component from the bottom margin of the parent component.
Horizontalspacing: Represents the horizontal spacing of child components.
Verticalspacing: Represents the vertical spacing of subassemblies.

related properties of Griddata

The flexibility of GridLayout layout is that it utilizes grid layout data griddata. By Griddata You can set the child component in the grid padding, size margin and other information, the user can set the Setlayoutdata method of the child component grid layout data.

Griddata can control the size of the child component's position in the grid, and other related display information. Griddata can set some of the following properties.

HorizontalAlignment: Represents the horizontal alignment.

VerticalAlignment: Represents the vertical alignment of a subassembly, with the same value as the horizontal method.
Horizontalindent: Indicates how many pixels the child component is offset horizontally. This property is used in conjunction with the "HorizontalAlignment = griddata.beginning" property.

Horizontalspan: Indicates that the component level occupies several grids.

Grabexcesshorizontalspace: Indicates whether the subassembly occupies space in a horizontal direction when the parent component size changes.
Grabexcessverticalspace: Indicates whether the subassembly occupies space in the vertical direction when the parent component size changes.
Widthhint: Indicates how many pixels the child component's width is (provided no other related properties are set).
Heighthint: Indicates how many pixels a child component's height is (provided no other related properties are set).

In addition, the griddata can specify the corresponding property value through the constructor, and interested readers can refer to the constructor of the Griddata class.

Test code:

Gridlayoutsample.java
Copy Code code as follows:

Package Swt_jface.demo2;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.layout.GridData;
Import Org.eclipse.swt.layout.GridLayout;
Import Org.eclipse.swt.widgets.Button;
Import Org.eclipse.swt.widgets.Display;
Import org.eclipse.swt.widgets.List;
Import Org.eclipse.swt.widgets.Shell;
public class Gridlayoutsample {

Display display = new display ();
Shell shell = new shell (display);
Public Gridlayoutsample () {

GridLayout GridLayout = new GridLayout ();
Gridlayout.numcolumns = 2;
Gridlayout.makecolumnsequalwidth = true;
Shell.setlayout (gridLayout);
Button button1 = New button (Shell, SWT. PUSH);
Button1.settext ("Button1");
Button1.setlayoutdata (New Griddata (griddata.vertical_align_beginning));

List List = new List (shell, SWT. BORDER);
List.add ("Item 1");
List.add ("Item 2");
List.add ("Item 3");
List.setlayoutdata (New Griddata (Griddata.vertical_align_center));

Button button2 = New button (Shell, SWT. PUSH);
Button2.settext ("button #2");
Griddata griddata = new Griddata (griddata.vertical_align_end);
Griddata.horizontalindent = 5;
Button2.setlayoutdata (Griddata);

Button Button3 = New button (Shell, SWT. PUSH);
Button3.settext ("3");
Button3.setlayoutdata (New Griddata (Griddata.vertical_align_fill));

Shell.pack ();
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New Gridlayoutsample ();
}
}

Gridlayoutsamplegrabspace.java
Copy Code code as follows:

Package Swt_jface.demo2;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.layout.GridData;
Import Org.eclipse.swt.layout.GridLayout;
Import Org.eclipse.swt.widgets.Button;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Label;
Import Org.eclipse.swt.widgets.Shell;
Import Org.eclipse.swt.widgets.Text;
public class Gridlayoutsamplegrabspace {
Public Gridlayoutsamplegrabspace () {

Display display = new display ();
Shell shell = new shell (display);

GridLayout GridLayout = new GridLayout ();
Gridlayout.numcolumns = 3;
Shell.setlayout (gridLayout);

Label label = new label (Shell, SWT. BORDER);
Label.settext ("label");

Griddata gridData3 = new Griddata ();
Griddata3.widthhint = 60;
Griddata3.heighthint = 20;

Label.setlayoutdata (GRIDDATA3);

Text text = new text (Shell, SWT. Single | Swt. BORDER);
Text.settext ("text");

Griddata griddata = new Griddata ();
Griddata.grabexcesshorizontalspace = true;
Griddata.grabexcessverticalspace = true;
Griddata.horizontalalignment = Griddata.fill;
Griddata.verticalalignment = Griddata.fill;
Text.setlayoutdata (Griddata);

Button button = New button (Shell, SWT. PUSH);
Button.settext ("button");

Griddata gridData2 = new Griddata ();
Griddata2.grabexcessverticalspace = true;
Griddata2.grabexcesshorizontalspace = true;
Griddata2.verticalalignment = Griddata.fill;
Griddata2.horizontalalignment = Griddata.fill;

Button.setlayoutdata (GRIDDATA2);

Shell.setsize (300, 80);
Shell.pack ();
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New Gridlayoutsamplegrabspace ();
}
}

Gridlayoutsamplespan.java
Copy Code code as follows:

Package Swt_jface.demo2;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.layout.GridData;
Import Org.eclipse.swt.layout.GridLayout;
Import Org.eclipse.swt.widgets.Button;
Import Org.eclipse.swt.widgets.Display;
Import org.eclipse.swt.widgets.List;
Import Org.eclipse.swt.widgets.Shell;
public class Gridlayoutsamplespan {

Display display = new display ();
Shell shell = new shell (display);
Public Gridlayoutsamplespan () {

GridLayout GridLayout = new GridLayout ();
Gridlayout.numcolumns = 2;
Gridlayout.makecolumnsequalwidth = true;
Shell.setlayout (gridLayout);
Button button1 = New button (Shell, SWT. PUSH);
Button1.settext ("Button1");
Button1.setlayoutdata (New Griddata (griddata.vertical_align_beginning));

List List = new List (shell, SWT. BORDER);
List.add ("Item 1");
List.add ("Item 2");
List.add ("Item 3");
List.setlayoutdata (New Griddata (Griddata.vertical_align_center));

Button button2 = New button (Shell, SWT. PUSH);
Button2.settext ("button #2");
Griddata griddata = new Griddata (griddata.vertical_align_end);
Griddata.horizontalspan = 2;
Griddata.horizontalalignment = Griddata.fill;
Button2.setlayoutdata (Griddata);

Button Button3 = New button (Shell, SWT. PUSH);
Button3.settext ("3");
Griddata gridData2 = new Griddata (griddata.vertical_align_end);
Griddata2.verticalspan = 3;
Button3.setlayoutdata (GRIDDATA2);

Shell.pack ();
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New Gridlayoutsamplespan ();
}
}

The following example layout is slightly more complicated:
Copy Code code as follows:

Package Swt_jface.demo2;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.custom.CLabel;
Import Org.eclipse.swt.graphics.Image;
Import Org.eclipse.swt.layout.GridData;
Import Org.eclipse.swt.layout.GridLayout;
Import Org.eclipse.swt.widgets.Button;
Import Org.eclipse.swt.widgets.Combo;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Label;
Import Org.eclipse.swt.widgets.Shell;
Import Org.eclipse.swt.widgets.Text;
public class Sample {

Display display = new display ();
Shell shell = new shell (display);
Public Sample () {
Shell.settext ("book Entry Demo");
GridLayout GridLayout = new GridLayout (4, false);
gridlayout.verticalspacing = 8;
Shell.setlayout (gridLayout);
Label label = new label (Shell, SWT. NULL);
Label.settext ("Title:");
Text title = new Text (Shell, SWT. Single | Swt. BORDER);
Griddata griddata = new Griddata (Griddata.horizontal_align_fill);
Griddata.horizontalspan = 3;
Title.setlayoutdata (Griddata);
Label = new label (Shell, SWT. NULL);
Label.settext ("Author (s):");
Text authors = new text (Shell, SWT. Single | Swt. BORDER);
Griddata = new Griddata (Griddata.horizontal_align_fill);
Griddata.horizontalspan = 3;
Authors.setlayoutdata (Griddata);
Label = new label (Shell, SWT. NULL);
Label.settext ("Cover:");
Griddata = new Griddata ();
Griddata.verticalspan = 3;
Label.setlayoutdata (Griddata);
Clabel cover = new Clabel (Shell, SWT. NULL);
Griddata = new Griddata (griddata.fill_horizontal);
Griddata.horizontalspan = 1;
Griddata.verticalspan = 3;
Griddata.heighthint = 100;
Griddata.widthhint = 100;
Cover.setlayoutdata (Griddata);
Label = new label (Shell, SWT. NULL);
Label.settext ("Pages");
Text pages = new text (Shell, SWT. Single | Swt. BORDER);
Pages.setlayoutdata (New Griddata (Griddata.horizontal_align_fill));
Label = new label (Shell, SWT. NULL);
Label.settext ("Publisher");
Text Pubisher = new text (Shell, SWT. Single | Swt. BORDER);
Pubisher.setlayoutdata (New Griddata (Griddata.horizontal_align_fill));
Label = new label (Shell, SWT. NULL);
Label.settext ("Rating");
Combo rating = new Combo (Shell, SWT. READ_ONLY);
Rating.setlayoutdata (New Griddata (Griddata.horizontal_align_fill));
Rating.add ("5");
Rating.add ("4");
Rating.add ("3");
Rating.add ("2");
Rating.add ("1");
Label = new label (Shell, SWT. NULL);
Label.settext ("Abstract:");
Text bookabstract =
New Text (
Shell
Swt. WRAP
| Swt. MULTI
| Swt. BORDER
| Swt. H_scroll
| Swt. V_scroll);
Griddata =
New Griddata (
Griddata.horizontal_align_fill | Griddata.vertical_align_fill);
Griddata.horizontalspan = 3;
Griddata.grabexcessverticalspace = true;
Bookabstract.setlayoutdata (Griddata);
Button Enter = New button (Shell, SWT. PUSH);
Enter.settext ("Enter");
Griddata = new Griddata ();
Griddata.horizontalspan = 4;
Griddata.horizontalalignment = Griddata.end;
Enter.setlayoutdata (Griddata);
Title.settext ("Professional Java interfaces with Swt/jface");
Authors.settext ("Jack Li Guojie");
Pages.settext ("500pp");
Pubisher.settext ("John Wiley & Sons");
Cover.setbackground (New Image (Display, "c:/eclipse32.gif"));
Bookabstract.settext (
"This book provides a comprehensive guide for \ n"
+ "To create Java user interfaces with Swt/jface");
Shell.pack ();
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New Sample ();
}
}

The function of GridLayout layout is very powerful, it is also a kind of layout method commonly used by the author. GridLayout is a grid layout that divides the parent component into a table, by default each subassembly occupies a cell's space, and each subassembly is arranged in the table in the order that it is added to the parent component.

GridLayout provides a number of properties that allow you to flexibly set the grid information. In addition, the GridLayout layout provides a Griddata class in which subcomponents can set up corresponding griddata, such as "Dogphoto.setlayoutdata (Griddata)", and Griddata can set information for each component as a cell.

The style of 14.11.1 GridLayout

The GridLayout class provides information for dividing the grid in the GridLayout layout, and is set mainly through the following several parameters.

NumColumns: The "Gridlayout.numcolumns" property allows you to set a number of columns in the parent component to display subcomponents, as shown in table 14-4.

Table 14-4 NumColumns Effect

Number of columns

Display effects

NumColumns = 1

498) this.style.width=498; "Border=0>

NumColumns = 2

498) this.style.width=498; "Border=0>

NumColumns = 3

498) this.style.width=498; "Border=0>


Makecolumnsequalwidth: Through "gridLayout." The Makecolumnsequalwidth property can set whether the parent component's neutron component has the same column width, and when Makecolumnsequalwidth is true, each column has an equal column width.

MarginLeft: Represents the number of pixel points in the current component from the left margin of the parent component.

MarginRight: Represents the number of pixel points in the current component from the right margin of the parent component.

MarginTop: Represents the number of pixels of the current component from the top margin of the parent component.

MarginBottom: Represents the number of pixel points in the current component from the bottom margin of the parent component.

Horizontalspacing: Represents the horizontal spacing of child components.

Verticalspacing: Represents the vertical spacing of subassemblies.

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.