Skip to content

ColorButton

greipadmin edited this page Feb 2, 2017 · 27 revisions

Introduction

Methods

  • setRGB(RGB) set the initial color value.
  • getRGB() gets the selected color or the initial color, when no color selected.
  • chooseRGB(IColorChooserFactory) for use inside a SelectionListener.
  • setColorChooserFactory(IColorChooserFactory) sets a factory for creating color choosers.
  • setResultConsumer(Consumer) sets a consumer for the selected color.
  • You can also use all SWT Button methods.

Color choosers

  • ColorChooserHSB, selects a color by hue, brightness and saturation.
  • ColorChooserRGB, selects a color by red, green, blue.
  • ColorWheel, selects a color from a color wheel.
  • ColorPicker, selects a color from a predefined or user defined color list.
  • Calculator, a nice calculator for text widgets or as whole widget.

Example: Use a Listener or SelectionListener

...
final ColorButton colorButton = new ColorButton(parent);
colorButton.setText("Click me!");

// initialize with current background rgb
colorButton.setRGB(shell.getBackground().getRGB());

colorButton.addListener(SWT.Selection, e -> {
	final RGB rgb = colorButton.chooseRGB(p -> new ColorChooserHSB(p, ColorResolution.Medium, true));
	// check if new color selected
	if (rgb != null) {
		// enter your own code here
	}
});
...

Example: ColorButton without Listeners

...
final ColorButton colorButton = new ColorButton(parent);
colorButton.setText("Click me!");

// initialize with current background rgb
colorButton.setRGB(shell.getBackground().getRGB());

colorButton.setColorChooserFactory(p -> new ColorWheelChooser(p, ColorResolution.Minimal, false));
colorButton.setColorConsumer(rgb -> {
	// enter your own code here
});
...
Clone this wiki locally