StyledLabel - greipadmin/greip GitHub Wiki

Introduction

The StyledLabel widget represents a user interface object that displays a formatted text and/or an image.

Constructor

  • StyledLabel(Composite parent, int style)
    • parent the parent Composite
    • style the style flags
      • the alignment style flags SWT.LEFT, SWT.CENTER, SWT.RIGHT and Greip.JUSTIFY
      • you can also use all style flags from SWT Label control

Methods

  • setAlignment(int) sets the text alignment (SWT.LEFT, SWT.CENTER, SWT.RIGHT and Greip.JUSTIFY)
  • int getAlignment() returns the current text alignment
  • setWrap(boolean) enables or disables automatic line wrapping
  • boolean isWrap() gets the current line wrap behaviour
  • addSelectionListener(SelectionListener) the listener is called by clicking links
  • removeSelectionListener(SelectionListener) removes the listener from the list of listeners
  • setExceptionHandler(Consumer<ParseException>) the handler is called by parser exceptions
  • you can also use all methods defined by SWT Label control

Text formatting

The text can contains some pseudo-HTML tags for formatting:

  • <a href="{id}"> to define a link. The attribute href is optional and accessible from selection listeners.
  • <b> to render text in bold
  • <br/> for adding a line break (platform dependent line breaks are also supported)
  • <i> to render text in italic
  • <s> to render strikeout text
  • <style fg="{color}" bg="{color}" size="{size}" font="{name}"> to define text foreground and background color as HTML color code (e.g. #FFAABB) the font size in pixels and the font name. All attributes are optional.
  • <sub> to render subscript text
  • <sup> to render superscript text
  • <u> to render text in underline

Example

public static void main(final String[] args) {
  final Display display = new Display();
  final Shell shell = new Shell(display);

  shell.setLayout(new GridLayout());
  shell.setText("Greip - StyledLabel Demo");

  final StyledLabel label = new StyledLabel(shell, SWT.CENTER | SWT.SHADOW_ETCHED_IN | SWT.BORDER);
  label.setLayoutData(new GridData(280, SWT.DEFAULT));
  label.setText("Hello <b>world</b>!\n<a href=\"1\">more...</a>");

  label.addListener(SWT.Selection, e -> {
    final MessageBox msgBox = new MessageBox(shell);
    final Link link = (Link) e.data;

    msgBox.setMessage("Link href=\"" + link.href + "\"");
    msgBox.open();
  });

  shell.pack();
  shell.open();

  while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) display.sleep();
  }
  display.dispose();
}
⚠️ **GitHub.com Fallback** ⚠️