Skip to content

FontButton

greipadmin edited this page Jan 5, 2019 · 7 revisions

Introduction

The FontButton widget allow the user to select a font from all available fonts in the system.

Constructor

  • FontButton(Composite parent, int style)
    • parent a widget which will be the parent of the new widget.
    • style the style of widget to construct.

Methods

  • FontData getFontData() returns the selected font data object.
  • void setFontData(final FontData fontData) sets the initial font data. If no font data is set the system font is used.
  • RGB getFontColor() gets the selected font color or null if no color chooser factory is set.
  • void setFontColor(final RGB fontColor) sets the initial font color. The font color is only used when a color chooser factory is defined.
  • void setColorChooserFactory(final IColorChooserFactory factory) sets the factory for creating the color chooser.
  • boolean chooseFont() opens the font selection popup (for use inside a selection listener).
  • void setDropDownArrowVisible(boolean) switch visibility of drop down arrow.
  • boolean isDropDownArrowVisible() returns the visibility of drop down arrow.

Example: select font only

...
final FontButton button = new FontButton(shell, SWT.NONE);
button.setText("  Select Font  ");
button.setFontConsumer(fd -> {
  [ consume selected font data here ]
});
...

Example: select font and color

...
final FontButton button = new FontButton(shell, SWT.NONE);
button.setText("  Select Font  ");
button.setColorChooserFactory(new ColorChooserRGB.Factory(ColorResolution.Minimal));
button.setFontConsumer((fd, rgb) -> {
  [ consume selected font data and color here ]
});
...

Example: handling font selection with SelectionListener

...
final FontButton button = new FontButton(shell, SWT.NONE);
button.setText("  Select Font  ");

// allow color selection
button.setColorChooserFactory(new ColorChooserHSB.Factory(ColorResolution.Maximal, true, true));

// add selection listener
button.addListener(SWT.Selection, e -> {
  // sets the initial font and color
  button.setFontData(shell.getFont().getFontData()[0]);
  button.setFontColor(shell.getForeground().getRGB());

  // open the font chooser popup
  if (chooseFont()) {
    // get selected font and color
    final FontData fd = button.getFontData();
    final RGB rgb = button.getFontColor();

    [ consume selected font data and color here ]
  }
});
...
Clone this wiki locally