View RSS Feed

My Java Tips

SWT List

Rate this Entry
by , 11-10-2011 at 07:07 PM (1613 Views)
List is a selectable user interface object that displays a list of strings and issues notification when a string selected. A list may be single or multi select. Events associated with List are Selection, DefaultSelection. Please note that only one of SINGLE and MULTI style may be specified.


The following are the important constructors of List class.

List(Composite parent, int style)
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

The following are the important methods of List class.

void add(String string)
Adds the argument to the end of the receiver's list.

void add(String string, int index)
Adds the argument to the receiver's list at the given zero-relative index.

int getFocusIndex()
Returns the zero-relative index of the item which currently has the focus in the receiver, or -1 if no item has focus.

String getItem(int index)
Returns the item at the given, zero-relative index in the receiver.

int getItemCount()
Returns the number of items contained in the receiver.

String[] getItems()
Returns a (possibly empty) array of Strings which are the items in the receiver.

String[] getSelection()
Returns an array of Strings that are currently selected in the receiver.

int getSelectionCount()
Returns the number of selected items contained in the receiver.

int indexOf(String string)
Gets the index of an item.

int indexOf(String string, int start)
Searches the receiver's list starting at the given, zero-relative index until an item is found that is equal to the argument, and returns the index of that item.

boolean isSelected(int index)
Returns true if the item is selected, and false otherwise.
void remove(int index)
Removes the item from the receiver at the given zero-relative index.

void remove(int[] indices)
Removes the items from the receiver at the given zero-relative indices.

void remove(int start, int end)
Removes the items from the receiver which are between the given zero-relative start and end indices (inclusive).

void remove(String string)
Searches the receiver's list starting at the first item until an item is found that is equal to the argument, and removes that item from the list.

void removeAll()
Removes all of the items from the receiver.

void select(int index)
Selects the item at the given zero-relative index in the receiver's list.

void select(int[] indices)
Selects the items at the given zero-relative indices in the receiver.

void select(int start, int end)
Selects the items in the range specified by the given zero-relative indices in the receiver.

void selectAll()
Selects all of the items in the receiver.

void setFont(Font font)
Sets the font that the receiver will use to paint textual information to the font specified by the argument, or to the default font for that kind of control if the argument is null.

void setItem(int index, String string)
Sets the text of the item in the receiver's list at the given zero-relative index to the string argument.

void setItems(String[] items)
Sets the receiver's items to be the given array of items.

void setSelection(int index)
Selects the item at the given zero-relative index in the receiver.

void setSelection(int[] indices)
Selects the items at the given zero-relative indices in the receiver.

void setSelection(int start, int end)
Selects the items in the range specified by the given zero-relative indices in the receiver.

void setSelection(String[] items)
Sets the receiver's selection to be the given array of items.

void showSelection()
Shows the selection.

Example

The example below creates a list of numbers from 0 to 127. You can select a single number or multiple numbers by pressing control key. The number(s) which you have selected will be shown on the console.

Java Code:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class SWTList {
	public static void main (String [] args) {
		Display display = new Display ();
		Shell shell = new Shell (display);
		final List list = new List (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
		for (int i=0; i<128; i++) list.add ("Item " + i);
		list.setBounds (0, 0, 100, 100);
		list.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				String string = "";
				int [] selection = list.getSelectionIndices ();
				for (int i=0; i
				System.out.println ("Selection={" + string + "}");
			}
		});
		list.addListener (SWT.DefaultSelection, new Listener () {
			public void handleEvent (Event e) {
				String string = "";
				int [] selection = list.getSelectionIndices ();
				for (int i=0; i				System.out.println ("DefaultSelection={" + string + "}");
			}
		});
		shell.pack ();
		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}

}

Submit "SWT List" to Facebook Submit "SWT List" to Digg Submit "SWT List" to del.icio.us Submit "SWT List" to StumbleUpon Submit "SWT List" to Google

Categories
SWT / JFace

Comments