View RSS Feed

My Java Tips

SWT Combo

Rate this Entry
by , 11-10-2011 at 06:50 PM (2861 Views)
Combo are controls that allow the user to choose an item from a list of items, or optionally enter a new value by typing it into an editable text field. Often, Combos are used in the same place where a single selection List widget could be used but space is limited. A Combo takes less space than a List widget and shows similar information.


The following are the important constructors of Combo class.

Combo(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 Combo 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.

void clearSelection()
Sets the selection in the receiver's text field to an empty selection starting just before the first character.

void copy()
Copies the selected text.

void cut()
Cuts the selected text.

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

void deselectAll()
Deselects all selected items in the receiver's list.

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

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

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

Point getSelection()
Returns a Point whose x coordinate is the character position representing the start of the selection in the receiver's text field, and whose y coordinate is the character position representing the end of the selection.

int getSelectionIndex()
Returns the zero-relative index of the item which is currently selected in the receiver's list, or -1 if no item is selected.

String getText()
Returns a string containing a copy of the contents of the receiver's text field, or an empty string if there are no contents.

int getTextLimit()
Returns the maximum number of characters that the receiver's text field is capable of holding.

int indexOf(String string)
Searches the receiver's list starting at the first item (index 0) until an item is found that is equal to the argument, and returns the index of that 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.

void paste()
Pastes text from clipboard.

void remove(int index)
Removes the item from the receiver's list at the given zero-relative index.

void remove(int start, int end)
Removes the items from the receiver's list 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's list and clear the contents of receiver's text field.

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

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 list to be the given array of items.

void setSelection(Point selection)
Sets the selection in the receiver's text field to the range specified by the argument whose x coordinate is the start of the selection and whose y coordinate is the end of the selection.

void setText(String string)
Sets the contents of the receiver's text field to the given string.

void setTextLimit(int limit)
Sets the maximum number of characters that the receiver's text field is capable of holding to be the argument.

Example

The example below creates a non editable combo. It opens drop down menu with items A, B, and C. A user can select either of this, that is either A or B or C.

Java Code:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class SWTCombo {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		Combo combo = new Combo(shell, SWT.READ_ONLY);
		combo.setItems(new String[] { "A", "B", "C" });
		combo.setSize(200, 200);
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}

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

Tags: combo, controls, swt Add / Edit Tags
Categories
SWT / JFace

Comments