gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




Vote for the Developer.com Product of the Year Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Red Hat Fedora Claims It's the Leader in Linux    November 21, 2008
Dos and Don'ts of SOA Data Access    November 20, 2008
Mandrake Linux Founder Back, Virtually    November 20, 2008
Amazon: We're a Technology Company    November 19, 2008
Free Tech Newsletter -

Constructing SWT Layouts
By Koray Guclu

Go to page: Prev  1  2  3  4  5  Next  

GridLayout

GridLayout is the most useful and flexible layout.The GridLayout lays out the widgets in grids. Configureable properties of the GridLayout are listed in Table 4.

The properties listed in Table 4 affect all the layout behavior. The GridLayout provides a data object as well. If you need to set a different property for a cell, you need to create a GridData object and set this as the control's layout data object.

The makeColumndEqualWidth property in Table 4 makes all the controls the same size, as shown in Figure 8. This property makes the columns the same size. If you want the widgets also to be the same size, you should need to use GridData object's horizontal/vertical alignment properties, as shown in Figure 12.

Figure 8. GridLayout.makeColumnnsEqualWidth=false,true

The numColumns field must be set. This field specifies the number of columns in the GridLayout. If the number of components added is bigger than the number of the components in a composite, GridLayout adds the component to a new row.

For example, five components added to a GridLayout in Figure 9. The GridLayout in Figure 9 only has four columns. For that reason, button A5 will be added to a the second row.

Figure 9 shows the location of the properties on the UI that are listed in Table 4.

Figure 9. GridLayout Example

Initially, each column can have a different width. If you want to force all columns to be the same size, you must set the makeColumnsEqualWidth variable to true, as shown in Figure 8.

 Listing 6.  GridLayout example
  // ...
  GridLayout gridLayout = new GridLayout();
  shell.setLayout(gridLayout);
  gridLayout.numColumns = 5;
  for (int i = 1; i <= 7; i++)
  {
    Button button = new Button(shell, SWT.PUSH);
    button.setText("A" + i);
  }
  // ...

The preceding code snippet creates a new grid layout and sets the number of columns to 5. Afterwards, seven components are added to the composite. The figure below shows the position of the widgets.

Figure 10. GridLayout Example using default properties

Advantages:

  • It is the most powerful layout
  • It puts widgets in rows and columns
  • It provides configurable margins
  • It provides configurable spaces
  • It provides a GridData object

GridData

Each widget within a composite having GridLayout can have GridData set a different property for each widget. Configureable properties of the GridData object are listed in Table 5.

Table 5. GridData properties
Variable Default Description
grabExcessHorizontalSpace
grabExcessVerticalSpace
false If true, after resize, the widget will grow enough to fit the remaining space.
heightHint
widthHint
SWT.DEFAULT (indicates that no minimum width is specified.) Specifies a minimum width/height for the column.
horizontalAlignment
verticalAlignment
GridData.BEGINNING(possible values are BEGINNING, CENTER, END, FILL) Specifies how controls will be positioned horizontally/vertically within a cell.
horizontalIndent 0 Specifies the number of pixels of indentation that will be placed along the left side of the cell.
horizontalSpan
verticalSpan
1 Specifies the number of column/row cells that the control will take up.

The effect of the first property listed in Table 5 is illustrated below. The grabExcessHorizontalSpace (or grabExcessVerticalSpace) property can be hard to understand at first. For that reason, the area that is affected by this property is highlighted in the picture below. As you can see, if this property is set to true, the width of the grids will be as large as possible. If you want a widget to fill the horizontal space, you need to use this in combination with the alignment property shown in Figure 12.

Figure 11. GridData.grabExcessHorizontalSpace=false,true

The horizontalAlignment (or verticalAlignment) property sets the alignment of the control. You can use this in combination with the grabExcessHorizontalSpace property.

Figure 12. GridData.horizontalAlignment = GridData.BEGINNING, GridData.CENTER, GridData.END, GridData.FILL

The effect of the horizontalSpace is shown below.

Figure 13. GridData.horizontalSpan=1,2

Go to page: Prev  1  2  3  4  5  Next  

Previous article: SWT Programming with Eclipse


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Other Java Archives






internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers