Class 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: 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 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:
    • 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:
    • 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: