DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 8.8 Creating List Widgets

8.8.1 Problem

You need to display multiple items from which the user can select.

8.8.2 Solution

Use a list widget, which can display lists of selectable items. Here's a selection of the most popular list widget methods:


void add(String string)

Adds the argument to the end of the list widget's list


void add(String string, int index)

Adds the argument to the list widget's list at the given zero-relative index


void addSelectionListener(SelectionListener listener)

Adds the listener to the collection of listeners who are notified when the list widget's selection changes


String getItem(int index)

Returns the item at the given zero-relative index in the list widget


int getItemCount( )

Returns the number of items contained in the list widget


String[] getItems( )

Returns an array of Strings that are the items in the list widget


String[] getSelection( )

Returns an array of Strings that are currently selected in the list widget


int getSelectionCount( )

Returns the number of selected items contained in the list widget


int getSelectionIndex( )

Returns the zero-relative index of the item that is currently selected in the list widget, or -1 if no item is selected


int[] getSelectionIndices( )

Returns the zero-relative indices of the items that are currently selected in the list widget


void setItems(String[] items)

Sets the list widget's items to be the given array of items


void setSelection(int index)

Selects the item at the given zero-relative index in the list widget


void setSelection(int[] indices)

Selects the items at the given zero-relative indices in the list widget


void setSelection(String[] items)

Sets the list widget's selection to be the given array of items

8.8.3 Discussion

As an example, we'll add a multiple-selection list widget to a shell and get the user's selections with the getSelectionIndices method; this example is named ListApp and can be found at this book's site. Here's how to add a list widget to a shell:

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

public class ListClass {

    public static void main (String [] args) {
        Display display = new Display ( );
        Shell shell = new Shell (display);
        shell.setText("List Example");
        shell.setSize(300, 200);

        final List list = new List (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        list.setBounds(40, 20, 220, 100);
        for (int loopIndex = 0; loopIndex < 9; loopIndex++){ 
            list.add("Item Number " + loopIndex);
        }
        .
        .
        .

You can add a selection listener to the list widget, as shown in Example 8-6. In this example, we'll use the list widget's getSelectionIndices method to get an int array of the selected indices in the list widget. We'll display those selections in a text widget, as you see in the code.

Example 8-6. Using list widgets
package org.cookbook.ch08;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

public class ListClass {

    public static void main (String [] args) {
        Display display = new Display ( );
        Shell shell = new Shell (display);
        shell.setText("List Example");
        shell.setSize(300, 200);

        final List list = new List (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        list.setBounds(40, 20, 220, 100);
        for (int loopIndex = 0; loopIndex < 9; loopIndex++){ 
            list.add("Item Number " + loopIndex);
        }
        
        final Text text = new Text(shell, SWT.BORDER);
        text.setBounds(60, 130, 160, 25);
        
        list.addSelectionListener(new SelectionListener( )
        {
           public void widgetSelected(SelectionEvent event)
           {
                int [] selectedItems = list.getSelectionIndices ( );
                String outString = "";
                for (int loopIndex = 0; loopIndex < selectedItems.length; 
                       loopIndex++) outString += selectedItems[loopIndex] + " ";
                text.setText("Selected Items: " + outString);
           }

           public void widgetDefaultSelected(SelectionEvent event)
           {
            int [] selectedItems = list.getSelectionIndices ( );
            String outString = "";
            for (int loopIndex = 0; loopIndex < selectedItems.length; loopIndex++) 
                  outString += selectedItems[loopIndex] + " ";
            System.out.println ("Selected Items: " + outString);
           }
        });

        shell.open ( );
        while (!shell.isDisposed ( )) {
            if (!display.readAndDispatch ( )) display.sleep ( );
        }
        display.dispose ( );
    } 
}

The results appear in Figure 8-7, where the code is displaying the selections the user has made.

Figure 8-7. Using a list widget
figs/ecb_0807.gif


    Previous Section  < Day Day Up >  Next Section