View RSS Feed

My Java Tips

SWT Text

Rate this Entry
by , 11-10-2011 at 07:12 PM (1380 Views)
Text is a selectable user interface objects that allow the user to enter and modify text. Events associated with Text are: DefaultSelection, Modify, Verify. Different Text styles are CENTER, LEFT, MULTI, PASSWORD, SINGLE, RIGHT, READ_ONLY, WRAP. Please note that only one of the styles MULTI and SINGLE may be specified.

The following are the important constructors of Text class.

Text(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 Text class.

void append(String string)
Appends a string.

void clearSelection()
Clears the selection.

void copy()
Copies the selected text.

void cut()
Cuts the selected text.

int getCharCount()
Returns the number of characters.

char getEchoChar()
Returns the echo character.

boolean getEditable()
Returns the editable state.

int getLineCount()
Returns the number of lines.

Point getSelection()
Returns a Point whose x coordinate is the character position representing the start of the selected text, and whose y coordinate is the character position representing the end of the selection.

String getSelectionText()
Gets the selected text, or an empty string if there is no current selection.

String getText()
Returns the widget text.

String getText(int start, int end)
Returns a range of text.

void insert(String string)
Inserts a string.

void paste()
Pastes text from clipboard.

void selectAll()
Selects all the text in the receiver.

void setEchoChar(char echo)
Sets the echo character.

void setEditable(boolean editable)
Sets the editable state.

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 setOrientation(int orientation)
Sets the orientation of the receiver, which must be one of the constants SWT.LEFT_TO_RIGHT or SWT.RIGHT_TO_LEFT.

void setRedraw(boolean redraw)
If the argument is false, causes subsequent drawing operations in the receiver to be ignored.

void setSelection(int start)
Sets the selection.

void setSelection(int start, int end)
Sets the selection to the range specified by the given start and end indices.

void setSelection(Point selection)
Sets the selection to the range specified by the given point, where the x coordinate represents the start index and the y coordinate represents the end index.

void setTabs(int tabs)
Sets the number of tabs.

void setText(String string)
Sets the contents of the receiver to the given string.

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

void setTopIndex(int index)
Sets the zero-relative index of the line which is currently at the top of the receiver.

void showSelection()
Shows the selection.


The example below creates a text field in a window. When Window opens it will consists of text `ASDF` with all text selected.

Java Code:
import org.eclipse.swt.widgets.*;
public class SWTText {
	public static void main (String [] args) {
		Display display = new Display ();
		Shell shell = new Shell (display);
		Text text = new Text (shell, 0);
		text.setText ("ASDF");
		text.setSize (64, 32);
		text.selectAll ();
		shell.pack (); ();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch ()) display.sleep ();
		display.dispose ();


Submit "SWT Text" to Facebook Submit "SWT Text" to Digg Submit "SWT Text" to Submit "SWT Text" to StumbleUpon Submit "SWT Text" to Google