SwitchButton - lcaron/opal GitHub Wiki
Introduction
This is a switch button to pick one value.
Usage
A switch button is a different graphic representation of a checkbox button. So the API is similar to the Button widget. You can get or set the selection,add selection listeners, enable or disable the button...
final SwitchButton button = new SwitchButton(shell, SWT.NONE);
button.setText("Switch button...");
button.setTextForSelect("Selected...");
button.setTextForUnselect("Unselected...");
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent e) {
System.out.println("Before clicking, the selection was... " + button.getSelection());
}
@Override
public void widgetDefaultSelected(final SelectionEvent e) {
}
});
Customisation
By default, the button is grey, without border, with a blue background and 2 labels "On" and "Off". You can customize almost all elements of a switch button by setting the following properties :
textForSelect
: text associated to the selected state of the buttontextForUnselect
: text associated to the unselected state of the buttontext
: text displayed besides the buttonround
: if true, the switch button's corners are round. If false, the switch button's corners are square.borderColor
: if set, a border is displayed around the widget.focusColor
: Color of the button when the focus is on.selectedForegroundColor
andselectedBackgroundColor
: Color of the button (text color and background color) of the selected state of the button.unselectedForegroundColor
andunselectedBackgroundColor
: : text associated to the select state of the buttonbuttonBorderColor
,buttonBackgroundColor1
andbuttonBackgroundColor1
: Colors of the button (border and gradient)gap
: gap between the button and the associated textforeground, background
: foreground and background colors of the whole widget.font
: font of the widget
Examples
An example called SwitchButtonSnippet is available in the directory src/test/java/org/mihalis/opal/switchButton
This example is also available here : https://github.com/lcaron/opal/blob/master/src/test/java/org/mihalis/opal/switchButton/SwitchButtonSnippet.java