Package org.eclipse.jface.widgets

# Class LabelFactory

java.lang.Object
org.eclipse.jface.widgets.AbstractWidgetFactory<F,C,Composite>
org.eclipse.jface.widgets.AbstractControlFactory<LabelFactory,Label>
org.eclipse.jface.widgets.LabelFactory

public final class LabelFactory extends AbstractControlFactory<LabelFactory,Label>
This class provides a convenient shorthand for creating and initializing `Label`. This offers several benefits over creating Label normal way:

- The same factory can be used many times to create several Label instances
- The setters on LabelFactory all return "this", allowing them to be chained

Example usage:

     Label label = LabelFactory.newLabel(SWT.LEFT)//
                    .text("Label:") //
                    .layoutData(gridData) //
                    .create(parent);

The above example creates a Label with a text and aligns it left. Finally the label is created in "parent".

     LabelFactory labelFactory = LabelFactory.newLabel(SWT.LEFT);
     labelFactory.text("Label 1:").create(parent);
     labelFactory.text("Label 2:").create(parent);
     labelFactory.text("Label 3:").create(parent);

The above example creates three labels using the same instance of LabelFactory.

Since:
  3.18

## Method Summary

Modifier and Type
Method
Description
`LabelFactory`
`align(int alignment)`
Controls how text and images will be displayed in the receiver.
`LabelFactory`
`image(Image image)`
Sets the receiver's image to the argument, which may be null indicating that no image should be displayed.
`static LabelFactory`
`newLabel(int style)`
Creates a new LabelFactory with the given style.
`LabelFactory`
`text(String text)`
Sets the receiver's text.

### Methods inherited from class org.eclipse.jface.widgets.AbstractControlFactory

`background, enabled, font, foreground, layoutData, orientation, supplyLayoutData, tooltip`

### Methods inherited from class org.eclipse.jface.widgets.AbstractWidgetFactory

`addProperty, cast, create, data, data`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### newLabel

public static LabelFactory newLabel(int style)
Creates a new LabelFactory with the given style. Refer to `Label(Composite, int)` for possible styles.

Returns:
  a new LabelFactory instance

### text

public LabelFactory text(String text)
Sets the receiver's text.

This method sets the widget label. The label may include the mnemonic character and line delimiters.

Mnemonics are indicated by an '&' that causes the next character to be the mnemonic. When the user presses a key sequence that matches the mnemonic, focus is assigned to the control that follows the label. On most platforms, the mnemonic appears underlined but may be emphasised in a platform specific manner. The mnemonic indicator character '&' can be escaped by doubling it in the string, causing a single '&' to be displayed.

Parameters:
  `text` - the text
Returns:
  this
See Also:
  - `Label.setText(String)`

### image

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

Parameters:
  `image` - the image to display on the receiver (may be null)
Returns:
  this
See Also:
  - `Label.setImage(Image)`

### align

public LabelFactory align(int alignment)
Controls how text and images will be displayed in the receiver. The argument should be one of `LEFT`, `RIGHT` or `CENTER`. If the receiver is a `SEPARATOR` label, the argument is ignored and the alignment is not changed.

Parameters:
  `alignment` - the alignment
Returns:
  this
See Also:
  - `Label.setAlignment(int)`