Skip to main content

JavaFX Quick Tip: Printing to the console

Posted by joconner on April 12, 2009 at 9:30 PM PDT

Like Java, the JavaFX Script has its own println statement. You can use the println statement to print output to the console. This method has already been imported by default, so all you have to do is invoke it.

println("Hello, JavaFX!");

This, of course, simply prints "Hello, JavaFX!" to the console window. Simple right? There are, however, a few improvements in the JavaFX method that do not exist in the plain Java System.out.println method. The JavaFX println method has the ability to interpolate placeholder variables.

Imagine you want to invoke a print method to print "Hello, {friend}!" and {friend} is a variable for a different name that is determined on each method invocation.

var friends = ["John", "Jack", "Nick", "Matt"];
for(friend in friends) {
  println("Hello, {friend}!");

This code iterates over the sequence of friends and substitutes the {friend} text with an actual string from the sequence.

That's the JavaFX way to put together strings. And in my opinion, it beats string concatenation any day!

Related Topics >>


It is way to format string. Java also has a string formatting that is more powerful. To concatenate strings use whitespaces: "prefix:" "message" ":postfix"

Note: to cleanly print sequences, you have to use toString: def seq = [ "One", "Two", "Three" ]; println("Sequence: {seq}"); // Items are concatenated! println("Sequence: {seq.toString()}"); // Items are cleanly separated