2 Replies Latest reply: May 21, 2013 6:46 PM by jsmith RSS

    Showing text with a hyperlink

    se*328623*78
      I would like to show certain (dynamic generated) text to the user (a status message).
      This message can contain clickable hyperlinks. I know I can do that with a text field in an embedded web view, but it looks a bit heavy for my purposes (as far as I understand that would instantiate an embedded browser).
      Is there a simpler way to show to the user a text with just a hyperlink ?

      Edited by: se**** on May 21, 2013 1:43 PM
        • 1. Re: Showing text with a hyperlink
          James_D
          If all the text comprises a link, you can use a [url http://docs.oracle.com/javafx/2/api/javafx/scene/control/Hyperlink.html]Hyperlink. If only some of the text is a link, and you're using JavaFX 8, it might work to place the Hyperlink in a TextFlow.

          In JavaFX 2.2 you could try just laying out Text and Hyperlink objects yourself, but getting the layout looking ok might be tricky, depending on how complex the text is.

          In Java[FX] 8, something like
          import javafx.application.Application;
          import javafx.scene.Scene;
          import javafx.scene.control.Hyperlink;
          import javafx.scene.layout.StackPane;
          import javafx.scene.text.Text;
          import javafx.scene.text.TextFlow;
          import javafx.stage.Stage;
          
          public class TextFlowWithLink extends Application {
          
              public static void main(String[] args) {
                  launch(args);
              }
          
              @Override
              public void start(Stage primaryStage) {
                  StackPane root = new StackPane();
                  Text text1 = new Text("Some text here, and then a ");
                  Hyperlink link = new Hyperlink("link");
                  Text text2 = new Text(". And now some more text.");
                  TextFlow textFlow = new TextFlow(text1, link, text2);
                  root.getChildren().add(textFlow);
                  link.setOnAction(event -> System.out.println("Link was clicked."));
          
                  Scene scene = new Scene(root, 400, 200);
                  primaryStage.setScene(scene);
                  primaryStage.show();
              }
          }
          • 2. Re: Showing text with a hyperlink
            jsmith
            If you use a TextFlow, be careful you don't run into the following bug:
            https://javafx-jira.kenai.com/browse/RT-28485 "Hittesting inside TextFlow fails"

            If your text is pretty simple (and for a status line it probably is), another solution is to use a FlowPane similar to the createTextFlow method in the following example:
            https://gist.github.com/jewelsea/5036908
            An advantage of using a FlowPane is that it will also work with Java 7. A disadvantage is you don't get nice line breaks for multi-lined text, but if your status message containing the hyperlinks is just a single line, that won't matter.

            To do the same thing as James sample with a FlowPane you can use the following code (it's practically identical):
            @Override public void start(Stage primaryStage) {
                StackPane root = new StackPane();
                Text text1 = new Text("Some text here, and then a ");
                Hyperlink link = new Hyperlink("link");
                Text text2 = new Text(". And now some more text.");
                FlowPane textFlow = new FlowPane(text1, link, text2);
                root.getChildren().add(textFlow);
                link.setOnAction(new EventHandler<ActionEvent>() {
                  @Override public void handle(ActionEvent event) {
                     System.out.println("Link was clicked.");
                  }
                });
            
                Scene scene = new Scene(root, 400, 200);
                primaryStage.setScene(scene);
                primaryStage.show();
            }