SWT layout--GridLayout&&GridData

GridLayout grid layout is widely used in swt. When doing enterprise rcp, most of the layout is used on the page. Since GridLayout is a grid layout, it is similar to a grid on the interface, with a row divided into several columns, similar to a table. The number of columns in each row is not necessarily the same, nor is it necessarily aligned, which requires us to edit the GridData. When GridLayout executes the layout method, it also obtains the GridData of the children in the control for calculation.

 

Let's take a look at the layout of GridLayout.

 

Construction method:

public GridLayout () {}
//numColumns is the number of columns, makeColumnEqualsWidth is whether each column has the same width
public GridLayout (int numColumns, boolean makeColumnsEqualWidth) {}

 The properties in GridLayout basically describe the margin pixels and the number of rows of the layout.

	public int numColumns = 1;//The default is 1 column
	public boolean makeColumnsEqualWidth = false;//By default each column is not equal
 	public int marginWidth = 5;//The left and right margins are 5
 	public int marginHeight = 5;//The upper and lower margins are 5
	public int marginLeft = 0;//Left margin 0
	public int marginTop = 0;//Top margin 0
	public int marginRight = 0;//Right margin 0
	public int marginBottom = 0;//Bottom margin 0
 	public int horizontalSpacing = 5;//Horizontal Spacing 5
 	public int verticalSpacing = 5;//The upper and lower interval is 5

 

GridLayout works well with GridData.

/**
	 * verticalAlignment specifies the vertical position of the control
	 * The default value is CENTER.
	 * Possible values are: <ul>
	 *    <li>SWT.BEGINNING (or SWT.TOP):上部</li>
	 * <li>SWT.CENTER: middle</li>
	 *    <li>SWT.END (or SWT.BOTTOM): 底部</li>
	 * <li>SWT.FILL: fill up and down</li>
	 * </ul>
	 */
	public int verticalAlignment = CENTER;
	
	/**
	 * Specify the horizontal position
	 * Possible values are: <ul>
	 *    <li>SWT.BEGINNING (or SWT.LEFT): Position the control at the left of the cell</li>
	 *    <li>SWT.CENTER: Position the control in the horizontal center of the cell</li>
	 *    <li>SWT.END (or SWT.RIGHT): Position the control at the right of the cell</li>
	 *    <li>SWT.FILL: Resize the control to fill the cell horizontally</li>
	 * </ul>
	 */
	public int horizontalAlignment = BEGINNING;
	public int widthHint = SWT.DEFAULT;//width
	public int heightHint = SWT.DEFAULT;//高度
	public int horizontalIndent = 0;//horizontal indent
	public int verticalIndent = 0;//Extend vertically downward
	public int horizontalSpan = 1;//Number of cells in the horizontal direction
	public int verticalSpan = 1;//Number of grids in the vertical direction
        //If the parent control has remaining space in the horizontal direction, if it is true, the control will be filled horizontally
	public boolean grabExcessHorizontalSpace = false;
        //Same as above, fill in the vertical direction
	public boolean grabExcessVerticalSpace = false;
	public int minimumWidth = 0;
	public int minimumHeight = 0;
The rest of the properties are some int values

 In fact, GridData is a property that describes the location of the corresponding control. Don't forget, after new GridData(....), the corresponding obj should be

control.setLayoutData (Object layoutData);

 

Let's look at an example next.

The following figure is an example of GridData used in a work project

 



 


illustrate:

The upper container is divided into two, the left button and the right timeline zoom control. The container on the left is divided into seven buttons, each of which is the same size, which requires setting the width of each grid in the layout to be the same. One detail is that the query task has a certain distance from the left margin of the container, which also needs to be set here. The right container should be set to be aligned to the right, otherwise it will be pasted to the right of the left container. ok, paste the code.

 

//The parent container sets the layout to gridLayout

private void initTop(Composite comp) {
		topComp = new Composite(comp, SWT.BORDER);
		topComp.setLayout(new GridLayout(2,false));//The upper panel is set to two columns
		GridData topCompGridData = new GridData();
		topCompGridData.heightHint=60;//The height of the panel is 60
		topCompGridData.horizontalAlignment=SWT.FILL;//It can also be set with horizontalHint, the level is full
		topCompGridData.grabExcessHorizontalSpace=true;//This needs to be set, otherwise the upper panel will not be filled with the parent container, these two properties are matched
		topComp.setLayoutData (topCompGridData);
		
		Composite topLeft = new Composite(topComp, SWT.LEFT);//Left button container
		topLeft.setLayout(new GridLayout(7,true));//7 columns, each column has the same width
		GridData topRightGridData = new GridData(SWT.END,SWT.CENTER,true,false);
		
		Composite topRight = new Composite(topComp,SWT.RIGHT);
		topRight.setLayoutData(topRightGridData);
		topRight.setLayout(new GridLayout(2,false));//Two columns with different widths
		
		// query button
		{
			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			gd.horizontalIndent = 10;//Horizontal indent 10 pixels
			queryTaskButton = new Button(topLeft,SWT.None);
			queryTaskButton.setLayoutData (gd);
			queryTaskButton.setText("Query Task");
			
			
		}
                {
                        // The rest of the buttons are not displayed
                 }
}	

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327039157&siteId=291194637