View RSS Feed

My Java Tips

SWT Label

Rate this Entry
by , 11-10-2011 at 06:02 PM (1204 Views)
A label displays a string or an image. The string can be changed by the application, but a user cannot edit it directly. For Label, when SEPARATOR is specified, displays a single vertical or horizontal line.

The following are the important constructors of Label class.

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

Point computeSize(int wHint, int hHint, boolean changed)
Returns the preferred size of the receiver.

int getAlignment()
Returns a value which describes the position of the text or image in the receiver.

Image getImage()
Returns the receiver's image if it has one, or null if it does not.

String getText()
Returns the receiver's text, which will be an empty string if it has never been set or if the receiver is a SEPARATOR label.

void setAlignment(int alignment)
Controls how text and images will be displayed in the receiver.

void setImage(Image image)
Sets the receiver's image to the argument, which may be null indicating that no image should be displayed.

void setText(String string)
Sets the receiver's text.

Different styles for Label are:


Please Note that only one of SHADOW_IN, SHADOW_OUT and SHADOW_NONE may be specified. Similarly, only one of HORIZONTAL and VERTICAL and only one of CENTER, LEFT and RIGHT may be specified.

To place a label on shell, use the following code:
Java Code:
Label label1 = new Label (shell, SWT.SEPARATOR | SWT.HORIZONTAL);
Label label2 = new Label (shell, SWT.SEPARATOR | SWT.VERTICAL);
Label label3 = new Label (shell, SWT.BORDER);

The example below creates empty label with horizontal and vertical separator

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

public class SWTLabel {
	public static void main (String [] args) {
		Display display = new Display ();
		Shell shell = new Shell (display);
		shell.setLayout (new FillLayout ());
		new Label (shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		new Label (shell, SWT.SEPARATOR | SWT.VERTICAL);
		shell.setSize (200, 200); ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		display.dispose ();


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

Tags: label, separator, swt Add / Edit Tags
SWT / JFace