Flylib.com

Books Software

 
 
 

7.4 Working with Composites and Layouts

     

7.4 Working with Composites and Layouts

Selecting a layout lets you specify how to arrange your controls; there are four layout classes built into SWT:


FillLayout

Lets you fill a shell's client area.


GridLayout

Lays out control children in a grid.


RowLayout

Lays out controls in either horizontal rows or vertical columns .


FormLayout

Lets you position controls relative to the parent composite or another control. It is the most precise of all the layouts.

For example, here's how the grid layout works. We're going to create a grid layout with four columns and fill it with buttons . In this case, we'll create a new Composite control, which can contain other controls, and fill the composite with buttons. To start, we create a new shell and use a row layout to display our composite control. Then we create the composite control and a grid layout in it with four columns (the SWT.NONE constant means we're not setting any nondefault styles here):

public static void main (String [] args) {

Display display = new Display ( );


final Shell shell = new Shell (display);


shell.setSize(300, 200);


shell.setLayout(new RowLayout( ));


final Composite composite = new Composite(shell, SWT.NONE);


GridLayout gridLayout = new GridLayout( );


gridLayout.numColumns = 4;


composite.setLayout(gridLayout);

.

    .

    .

All that's left is to add the buttons to the composite control using a loop and to add the event loop itself, as you see in Example 7-3.

Example 7-3. Using SWT layouts, Ch07_03.java
package org.eclipsebook.ch07;



import org.eclipse.swt.*;

import org.eclipse.swt.layout.*;

import org.eclipse.swt.widgets.*;



public class Ch07_03 {



        public static void main (String [] args) {

                Display display = new Display ( );

                final Shell shell = new Shell (display);

                shell.setSize(300, 200);

                shell.setLayout(new GridLayout( ));

                

                final Composite composite = new Composite(shell, SWT.NONE);

                GridLayout gridLayout = new GridLayout( );

                gridLayout.numColumns = 4;

                composite.setLayout(gridLayout);

for (int loopIndex = 0; loopIndex < 18; loopIndex++) {

                        Button button = new Button(composite, SWT.PUSH);

                        button.setText("Button " + loopIndex);

                }



                shell.open ( );

                while (!shell.isDisposed( )) {

                        if (!display.readAndDispatch( )) display.sleep( );

                }

                display.dispose ( );

}

}

You can see the results in Figure 7-3, where we've arranged our buttons using a grid layout.

Figure 7-3. Using a grid layout
figs/ecps_0703.gif

The form layout is relatively new and very powerful because it allows you to position controls where you want them relative to other controls or the container.

     

7.5 Working with Lists

Another popular control is the list control, represented by the SWT List class. We'll take a look at an example using this control and how to recover selections made in the control. Here are the styles you can use with lists:


SWT.BORDER

Adds a border


SWT.H_SCROLL

Adds a horizontal scrollbar


SWT.V_SCROLL

Adds a vertical scrollbar


SWT.SINGLE

Allows single selections


SWT.MULTI

Allows multiple selections

In this case, we're going to fill the entire shell with a list control, using the fill layout. Here's what that looks likenote that we're also using the List class's add method to add items to the list:

public class Ch07_04 {



        public static void main (String [] args) {

Display display = new Display ( );


Shell shell = new Shell (display);


shell.setText("List Example");


shell.setSize(300, 200);


shell.setLayout(new FillLayout(SWT.VERTICAL));


final List list = new List (shell, SWT.BORDER  SWT.MULTI  SWT.V_SCROLL);


for (int loopIndex = 0; loopIndex < 100; loopIndex++){


list.add("Item " + loopIndex);


}

.

        .

        .

Now we can recover the selected items using a selection listener and the getSelectionIndices method. This method returns an int array of the selected indices in the list control, and we can display those indices in the console, as you see in Example 7-4.

Example 7-4. Using lists
package org.eclipsebook.ch07;



import org.eclipse.swt.*;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.layout.*;

import org.eclipse.swt.events.*;



public class Ch07_04 {



        public static void main (String [] args) {

             Display display = new Display ( );

             Shell shell = new Shell (display);

             shell.setText("List Example");

             shell.setSize(300, 200);

             shell.setLayout(new FillLayout(SWT.VERTICAL));



             final List list = new List (shell, SWT.BORDER  SWT.MULTI  SWT.V_SCROLL);

                

             for (int loopIndex = 0; loopIndex < 100; loopIndex++){ 

                 list.add("Item " + loopIndex);

                }

list.addSelectionListener(new SelectionListener( )

             {



                public void widgetSelected(SelectionEvent event)

                {

                     int [] selections = list.getSelectionIndices ( );

                     String outText = "";

                     for (int loopIndex = 0; loopIndex < selections.length; 

                         loopIndex++) outText += selections[loopIndex] + " ";

                     System.out.println ("You selected: " + outText);

                   }



                public void widgetDefaultSelected(SelectionEvent event)

                {

                     int [] selections = list.getSelectionIndices ( );

                     String outText = "";

                     for (int loopIndex = 0; loopIndex < selections.length; loopIndex++) 

                         outText += selections[loopIndex] + " ";

                    System.out.println ("You selected: " + outText);

                  }

               });

shell.open ( );

               while (!shell.isDisposed ( )) {

                   if (!display.readAndDispatch ( )) display.sleep ( );

               }

               display.dispose ( );

        } 

}

You can see the results in Figure 7-4, where we're displaying the selections the user made in the list control.

Figure 7-4. Selecting items in a list
figs/ecps_0704.gif