Class ButtonFactory
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 forSelectionEvent
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 TypeMethodDescriptionSets the receiver's image to the argument, which may benull
indicating that no image should be displayed.static ButtonFactory
newButton
(int style) Creates a new ButtonFactory with the given style.onSelect
(Consumer<SelectionEvent> consumer) Creates aSelectionListener
and registers it for the widgetSelected event.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
-
Method Details
-
newButton
Creates a new ButtonFactory with the given style. Refer toButton(Composite, int)
for possible styles.- Returns:
- a new ButtonFactory instance
-
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
Sets the receiver's image to the argument, which may benull
indicating that no image should be displayed.- Parameters:
image
- the image to display on the receiver (may benull
)- Returns:
- this
- See Also:
-
onSelect
Creates aSelectionListener
and registers it for the widgetSelected event. If the receiver is selected by the user the given consumer is invoked. TheSelectionEvent
is passed to the consumer.- Parameters:
consumer
- the consumer whose accept method is called- Returns:
- this
- See Also:
-