Skip to content

StyledLabel

greipadmin edited this page Feb 23, 2017 · 13 revisions

Introduction

The StyledLabel widget represents a label with text formatting.

Constructor

  • StyledLabel(Composite parent, int style)
  • parent the parent Composite
  • style the style flags. You can use all style flags defined by SWT Label widget.

Methods

All methods identical to SWT Label widget.

Text formatting

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

  • <br/> for adding a line break
  • <i>...</i> to render text in italic
  • <u>...</u> to render text in underline
  • <b>...</b> to render text in bold
  • <s>...</s> to render strikeout text
  • <style fg="{color}" bg="{color}" size="{size}">...</style> to define text foreground and background color as HTML color code (e.g. #FFAABB) and fontsize in pixels. All attributes are optional.
  • <link id="{id}" url="{url}">...</link> to define a link. The attributes id and url are accessible from selection listener.

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<link id=\"1\">more...</link>");

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

		msgBox.setMessage("Link id: \"" + link.getId() + "\"");
		msgBox.open();
	});

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

	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) {
			display.sleep();
		}
	}
	display.dispose();
}
Clone this wiki locally