Hello, I am trying to create automatic highlighting of hyperlinks inspired by RichTextFX demos. However, I need highlighted text to be clickable. Let's say simple print of URL would be enough. For example if I write www.github.com into CodeArea it will be highlighted and whenever I click on it "Go to www.github.com" will appear. Is it possible to do something like this in RichTextFX? Thanks for any replies.
Here is a sample of color highlighting:
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.StyleSpans;
import org.fxmisc.richtext.StyleSpansBuilder;
public class Main extends Application {
private static final Pattern PATTERN = Pattern.compile("w{3}(\\.\\w+/?)+"); // Simple regex to hightlight www.github.com
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
CodeArea codeArea = new CodeArea();
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
codeArea.richChanges().subscribe(change -> codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText())));
Scene scene = new Scene(new StackPane(codeArea), 600, 400);
Application.setUserAgentStylesheet(getClass().getResource("style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Hyperlink Demo");
primaryStage.show();
}
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
Matcher matcher = PATTERN.matcher(text);
int lastKwEnd = 0;
StyleSpansBuilder<Collection<String>> spansBuilder
= new StyleSpansBuilder<>();
while (matcher.find()) {
String styleClass ="hyperlink";
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}
}
Is there any way I could apply my own style instead of css "hyperlink" style from previous example? Something like this:
static class Hyperlink {
static final Hyperlink NO_LINK = new Hyperlink();
private final String url;
private Hyperlink() {
this(null);
}
Hyperlink(String url) {
this.url = url;
}
void applyToText(Text text) {
if(url != null) {
text.setOnMouseClicked(click -> {
System.out.println("Go to " + url);
});
}
}
}
spansBuilder.add(Collections.singleton(new Hyperlink(matcher.group(0))), matcher.end() - matcher.start());
Hello, I am trying to create automatic highlighting of hyperlinks inspired by RichTextFX demos. However, I need highlighted text to be clickable. Let's say simple print of URL would be enough. For example if I write www.github.com into CodeArea it will be highlighted and whenever I click on it "Go to www.github.com" will appear. Is it possible to do something like this in RichTextFX? Thanks for any replies.
Here is a sample of color highlighting:
Is there any way I could apply my own style instead of css "hyperlink" style from previous example? Something like this: