Over a million developers have joined DZone.

Simple JavaFX 2.0 Text Example

DZone's Guide to

Simple JavaFX 2.0 Text Example

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Oracle announced at JavaOne 2010 that they would be deprecating JavaFX Script and changing JavaFX to support standard Java APIs. In my previous post Hello JavaFX 2.0: Introduction by Command Line, I demonstrated that JavaFX 2.0 has definitely put the 'Java' back into JavaFX. This post continues that theme, showing examples of rendering various text effects, fonts, and sizes via JavaFX 2.0 using "pure Java" APIs. The example in this post can be compiled with the javac compiler and run with the java application launcher (assuming appropriate JavaFX library is available in classpath in both cases).

The JavaFX APIs for programmatically handling text in JavaFX 2.0 are easy to use for basic applications, but provide support for more sophisticated effects as the developer becomes comfortable with the APIs. The examples shown in the single application featured in this post are relatively straightforward and likely require little explanation.

package dustin.examples;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

 * Simple demonstration of JavaFX text support.
 * @author Dustin
public class JavaFxText extends Application
    * Start demonstration of JavaFX text capabilities.
    * @param stage Stage for JavaFX application.
    * @throws Exception Exception in JavaFX application.
   public void start(final Stage stage) throws Exception
      stage.setTitle("Simplistic Example of JavaFX 2.0 Text Capabilities");
      final Group rootGroup = new Group();
      final Scene scene =
         new Scene(rootGroup, 800, 400, Color.BEIGE);
      final Text text1 = new Text(25, 25, "(2007) JavaFX based on F3");
      text1.setFont(Font.font(java.awt.Font.SERIF, 25));

      final Text text2 = new Text(25, 50, "(2010) JavaFX Script Deprecated");
      text2.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));

      final Text text3 = new Text(25, 75, "(2011) JavaFX to be Open Sourced!");
      text3.setFont(Font.font(java.awt.Font.MONOSPACED, 35));

      final Text text4 = new Text(25, 125, "(2011) JavaFX to be Standardized");
      text4.setFont(Font.font(java.awt.Font.DIALOG, 40));
      final Effect glow = new Glow(1.0);

      final Text text5 = new Text(25, 175, "(Now) Time for JavaFX 2.0!");
      text5.setFont(Font.font(java.awt.Font.SERIF, FontWeight.EXTRA_BOLD, 45));
      final Light.Distant light = new Light.Distant();
      final Lighting lighting = new Lighting();

      final Text text6 = new Text(25, 225, "JavaFX News at JavaOne!");
      text6.setFont(Font.font(java.awt.Font.DIALOG_INPUT, FontWeight.THIN, 45));
      final Reflection reflection = new Reflection();

    * Main JavaFX application launching method.
    * @param arguments Command-line arguments: none expected.
   public static void main(final String[] arguments)

All of the interesting code occurs in the overridden start(Stage) method of this class that extends Application. Different effects (glow, lighting, reflection), font sizes, and font types are demonstrated in this example. The output from running this application from the command line is shown next.

The example highlighted in this post demonstrates a subset of JavaFX 2.0's support for text rendering. It also is another example of how JavaFX 2.0 has made JavaFX readily available via Java APIs.


From http://marxsoftware.blogspot.com/2011/12/simple-javafx-20-text-example.html

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}