Class WidgetFactory

java.lang.Object
org.eclipse.jface.widgets.WidgetFactory

public final class WidgetFactory extends Object
This class provides a convenient shorthand for creating and initializing factories for SWT widgets. This offers several benefits over creating SWT widgets with the low level SWT API
  • The same factory can be used many times to create several widget instances
  • The setters can be chained
  • Factories accept a lambda whenever applicable
Example usage:
 Button button = WidgetFactory.button(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 = WidgetFactory.button(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 button.

Since:
3.18
  • Method Details

    • button

      public static ButtonFactory button(int style)
      Parameters:
      style - SWT style applicable for Button. Refer to Button(Composite, int) for supported styles.
      Returns:
      ButtonFactory
    • text

      public static TextFactory text(int style)
      Parameters:
      style - SWT style applicable for Text. Refer to Text(Composite, int) for supported styles.
      Returns:
      TextFactory
    • label

      public static LabelFactory label(int style)
      Parameters:
      style - SWT style applicable for Label. Refer to Label(Composite, int) for supported styles.
      Returns:
      LabelFactory
    • composite

      public static CompositeFactory composite(int style)
      Parameters:
      style - SWT style applicable for Composite. Refer to Composite(Composite, int) for supported styles.
      Returns:
      CompositeFactory
    • dateTime

      public static DateTimeFactory dateTime(int style)
      Parameters:
      style - SWT style applicable for DateTime. Refer to DateTime(Composite, int) for supported styles.
      Returns:
      DateTimeFactory
      Since:
      3.22
    • spinner

      public static SpinnerFactory spinner(int style)
      Parameters:
      style - SWT style applicable for Spinner. Refer to Spinner(Composite, int) for supported styles.
      Returns:
      SpinnerFactory
    • table

      public static TableFactory table(int style)
      Parameters:
      style - SWT style applicable for Table. Refer to Table(Composite, int) for supported styles.
      Returns:
      TableFactory
    • tree

      public static TreeFactory tree(int style)
      Parameters:
      style - SWT style applicable for Tree. Refer to Tree(Composite, int) for supported styles.
      Returns:
      TreeFactory
    • tableColumn

      public static TableColumnFactory tableColumn(int style)
      Parameters:
      style - SWT style applicable for TableColumn. Refer to TableColumn(Table, int) for supported styles.
      Returns:
      TableColumnFactory
    • treeColumn

      public static TreeColumnFactory treeColumn(int style)
      Parameters:
      style - SWT style applicable for TreeColumn. Refer to TreeColumn(Tree, int) for supported styles.
      Returns:
      TreeColumnFactory
    • sash

      public static SashFactory sash(int style)
      Parameters:
      style - SWT style applicable for Sash. Refer to Sash(Composite, int) for supported styles.
      Returns:
      SashFactory
      Since:
      3.21
    • sashForm

      public static SashFormFactory sashForm(int style)
      Parameters:
      style - SWT style applicable for SashForm. Refer to SashForm(Composite, int) for supported styles.
      Returns:
      SashFormFactory
      Since:
      3.21
    • shell

      public static ShellFactory shell(int style)
      Parameters:
      style - SWT style applicable for Shell. Refer to Shell(Shell, int) for supported styles.
      Returns:
      ShellFactory
      Since:
      3.21
    • group

      public static GroupFactory group(int style)
      Parameters:
      style - SWT style applicable for Group. Refer to Group(Composite, int) for supported styles.
      Returns:
      GroupFactory
      Since:
      3.24
    • browser

      public static BrowserFactory browser(int style)
      Parameters:
      style - SWT style applicable for Browser. Refer to Browser(Composite, int) for supported styles.
      Returns:
      BrowserFactory
      Since:
      3.25