Can HTML style links be added to SWT StyledText?

I know that SWT has a Link class for creating href-style HTML links as widgets, but I tried to find a way to make certain text in the StyledText control appear and function as a link.

It seems to me that Eclipse does this in its code editor if you hold down the control key and hover over the name of the method, but I know that the Eclipse java editor is much more complicated than the StyledText control.

+6
java swt jface
source share
2 answers

With JFace 3.5 there is a special style for links:

styleRange.underlineStyle = SWT.UNDERLINE_LINK; styleRange.data = "http://www.google.com/"; 

This makes link identification easy, and you can keep the URL in style. As for the automatic search for links, just find the http://[^ ] pattern (spaces are not allowed in links) in the lines you get and add a style.

+7
source share

You need to add a LineStyleListener to the StyledText widget:

 textField.addLineStyleListener (...); ... public void lineGetStyle (LineStyleEvent e) { // alloc a set of styles for the requested line e.styles = new StyleRange [...]; for (int i = 0; i < e.styles.length; i++) { StyleRange styleRange = new StyleRange (); styleRange.start = ...; styleRange.length = ...; styleRange.underline = true; styleRange.foreground = <URL colour>; e.styles [i] = styleRange; } } 

Javadoc for LineStyleListener will provide you more information.

To add click behavior, you need one more logic: I could also insert some code that we use to automatically add the URL of the HTML style link as a StyledText widget if that helps.

+2
source share

All Articles