0

I tried implementing some existing solutions, which could not help me. A string is received from API "You can visit out website:http://localhost:0000/stack_overflow/". This whole string needs to be displayed. I want to split the String in such a way to make the "http://localhost:0000/stack_overflow/" part a hyperlink and clickable.

I tried using TextFlow and splitting the text into Text and Hyperlink, however, when I split using the ":", all the ":" come into play breaking the String.

String urlLink = "You can visit out website:http://localhost:0000/stack_overflow/";
TextFlow textFlow = new TextFlow();
ImageView imageView = new ImageView();
imageView.setImage(new Image(Resources.ICON));
String[]  information = urlLink.split(":");
Text      txtInfo     = new Text(information[0]);
Hyperlink link        = new Hyperlink(information[1]);
link.setOnAction(event -> {
   try {
    Runtime.getRuntime().exec("cmd.exe /c start iexplore " + link);
   } catch (IOException e) {
     e.printStackTrace();
   }
});
textFlow = new TextFlow(txtInfo, link);
wesvin
  • 45
  • 6
  • The string received may sometimes not have the spaces. And also when i split, i still want the ":" to be visible on the display. – wesvin Jun 04 '20 at 02:22
  • https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#split(java.lang.String,int) ? – James_D Jun 04 '20 at 12:33
  • 4
    So all that JavaFX stuff in your code is irrelevant to your question, how to `split` a string at the right location. The code example is not even consistent, as it begins with a declaration of a variable named `urlLink`, but then suddenly uses `description.getCaption()` instead of the variable. A simple solution is `urlLink.split("(?=https?:)");`, to split before the beginning of an `http:` or `https:` url. – Holger Jun 04 '20 at 14:06

2 Answers2

1

You can use the overloaded version of String.split() that allows you to specify a limit for the number of items in the split. (This is almost exactly the example in the first row in the table in the linked documentation.)

String[]  information = description.getCaption().split(":", 2);

To display the colon in the text, just concatenate it back in:

Text      txtInfo     = new Text(information[0] + ":");

Note that a better, platform-independent, way to show a URL in the system browser is to use getHostServices().showDocument(...). The getHostServices() method is available from your Application instance; for details on how to provide the HostServices to other parts of your application, see JavaFx 8: open a link in a browser without reference to Application.

String urlLink = "You can visit our website:http://localhost:0000/stack_overflow/";
TextFlow textFlow = new TextFlow();
String[]  information = description.getCaption().split(":", 2);
Text      txtInfo     = new Text(information[0] + ":");
Hyperlink link        = new Hyperlink(information[1]);
link.setOnAction(event -> getHostServices().showDocument(information[1]));
textFlow = new TextFlow(txtInfo, link);
James_D
  • 201,275
  • 16
  • 291
  • 322
0

You may solve the issue with the regex split on " : " (with spaces around the colon). (as suggested by @James_D in the comments)

Anyway, i'd like to point out a good approach to call Java on opening a web page is

try {
    Desktop.getDesktop().browse(new URL("https://google.com").toURI());
} catch (IOException e) {
    e.printStackTrace();
} catch (URISyntaxException e) {
    e.printStackTrace();
}

You don't need Runtime at all.

EDIT: I assumed he is working with Java 8 (jre & jdk ) due to "java-8" tag and I have tailored my answer on that. As @James_D pointed out, is not valid in Java 9 and later, as you explicitly have to declare a dependency on the java.desktop module, which adds a lot of bulk if you are linking a specific image for your application.

Snix
  • 541
  • 4
  • 12
  • 1
    Don't mix UI toolkits unless you need to. `Desktop` is part of AWT, not JavaFX. Use `getHostServices().showDocument(...)` – James_D Jun 03 '20 at 23:00
  • `getHostServices().showDocument(...)` is not a good way to handle it. One of the problems here is that you introduce a dependency on your application type for all controllers that need access to the host services. You can check more here: (https://stackoverflow.com/questions/33094981/javafx-8-open-a-link-in-a-browser-without-reference-to-application) Anyway, i don't want to open a debate. Yes, this will introduce a dependency to the AWT stack. This is probably not an issue if you're working with the full JRE. – Snix Jun 03 '20 at 23:06
  • 1
    I'm pretty familiar with that post... Surely a dependency on a class you have written is better than a dependency on an entire toolkit. Your argument "if you're working with the full JRE" is not valid in Java 9 and later, as you explicitly have to declare a dependency on the `java.desktop` module, which adds a lot of bulk if you are linking a specific image for your application. – James_D Jun 03 '20 at 23:10
  • I assume he is working with Java 8 (jre & jdk ) if he uses "java-8" tag. I tailored my answer on that. Thanks anyway for your advice. – Snix Jun 03 '20 at 23:19
  • Well that’s a fair point - I hadn’t noticed that tag. Can you [edit] the question with a brief note to that effect? – James_D Jun 03 '20 at 23:20
  • Done, it should be more explicit now and visible – Snix Jun 03 '20 at 23:25