Package org.eclipse.jface.widgets

# Class ButtonFactory

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

public final class ButtonFactory extends AbstractControlFactory<ButtonFactory,Button>
This class provides a convenient shorthand for creating and initializing `Button`. This offers several benefits over creating with widget the normal way:

- The same factory can be used many times to create several instances
- The setters all return "this", allowing them to be chained
- The `onSelect(java.util.function.Consumer<org.eclipse.swt.events.SelectionEvent>)`) accepts a Lambda for `SelectionEvent`

Example usage:

     Button button = ButtonFactory.newButton(SWT.PUSH) //
                    .text("Click me!") //
                    .onSelect(event -> buttonClicked(event)) //
                    .layoutData(gridData) //
                    .create(parent);

The above example creates a push button with a text, registers a SelectionListener and finally creates the button in "parent".

     GridDataFactory gridDataFactory = GridDataFactory.swtDefaults();
     ButtonFactory buttonFactory = ButtonFactory.newButton(SWT.PUSH).onSelect(event -> buttonClicked(event))
                    .layout(gridDataFactory::create);
     buttonFactory.text("Button 1").create(parent);
     buttonFactory.text("Button 2").create(parent);
     buttonFactory.text("Button 3").create(parent);

The above example creates three buttons using the same instance of ButtonFactory. Note the layout method. A Supplier is used to create unique GridData for every single widget.

Since:
  3.18

## Method Summary

Modifier and Type
Method
Description
`ButtonFactory`
`image(Image image)`
Sets the receiver's image to the argument, which may be `null` indicating that no image should be displayed.
`static ButtonFactory`
`newButton(int style)`
Creates a new ButtonFactory with the given style.
`ButtonFactory`
`onSelect(Consumer<SelectionEvent> consumer)`
Creates a `SelectionListener` and registers it for the widgetSelected event.
`ButtonFactory`
`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

### newButton

public static ButtonFactory newButton(int style)
Creates a new ButtonFactory with the given style. Refer to `Button(Composite, int)` for possible styles.

Returns:
  a new ButtonFactory instance

### text

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

This method sets the button label. The label may include the mnemonic character but must not contain 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, a selection event occurs. On most platforms, the mnemonic appears underlined but may be emphasized 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:
  - `Button.setText(String)`

### image

public ButtonFactory 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:
  - `Button.setImage(Image)`

### onSelect

public ButtonFactory onSelect(Consumer<SelectionEvent> consumer)
Creates a `SelectionListener` and registers it for the widgetSelected event. If the receiver is selected by the user the given consumer is invoked. The `SelectionEvent` is passed to the consumer.

Parameters:
  `consumer` - the consumer whose accept method is called
Returns:
  this
See Also:
  - `Button.addSelectionListener(SelectionListener)`
  - `SelectionListener.widgetSelectedAdapter(Consumer)`