JavaFX Text - JavaFX

What is JavaFX Text?

Just like various shapes, you may also create a text node in JavaFX. The text node is represented by using the class named Text, which belongs to the package javafx.scene.text.

This class includes several properties to create text in JavaFX and adjust its look. This class also inherits the form class which belongs to the package javafx.scene.shape.

Therefore, further to the properties of the text like font, alignment, line spacing, text, etc. It also inherits the basic form node properties including strokeFill, stroke, strokeWidth, strokeType, etc.

Creating a Text Node

Since the class text of the package javafx.scene.text represents the text node in JavaFX, you may create a text through instantiating this class as follows –

The class text includes a assets named text of string type, which represents the text that is to be created.

After instantiating the text class, you need to set value to this property using the setText() technique as shown below.

You may also set the position (origin) of the text by using specifying the values to the properties x and y using their respective setter techniques namely setX() and setY() as proven inside the following code block –

Example

The following program is an example demonstrating how to create a text node in JavaFX. Save this code in a file with name TextExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window showing the specified text as follows –

JavaFX - Text

Position and Font of the Text

Through default, the text created by way of text class is of the font…, size…, and black in color.

You may change the font length and colour of the text the use of the setFont() technique. This technique accepts an item of the Font class.

The class named Font of the package javafx.scene.text is used to outline the font for the text. This class includes a static technique named font().

This technique accepts 4 parameters namely −

  • Family − that is of a String type and represents the family of the font that we want to use to the text.
  • Weight − This assets represents the weight of the font. It accepts 9 values, that are − FontWeight.BLACK, FontWeight.bold, FontWeight.EXTRA_BOLD, FontWeight.EXTRA_LIGHT, light, MEDIUM, normal, SEMI_BOLD, thin.
  • Posture − This assets represents the Font posture (regular or italic). It accepts two values FontPosture.Normal and FontPosture.ITALIC.
  • Size − This property is of type double and it represents the size of the font.

You may set font to the text through using the following technique –

Example

The following program is an example demonstrating how to set font of the text node in JavaFX. In here, we are setting the font to Verdana, weight to ambitious, posture to regular and size to 20.

Save this code in a record with the name TextFontExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window showing the text with the specified font as follows –

JavaFX - Text

Stroke And Color

The text class also inherits the class form of the package. therefore, you may use javafx.scene.shape with which you may set the stroke and colour to the text node too.

You may set the colour to the text using the setFill() technique of the form (inherited) class as follows –

Similarly, you may set the stroke color of the text using the technique setStroke(). even as the width of the stroke may be set the usage of the technique setStrokeWidth() as follows –

Example

The following program is an example that demonstrates the way to set the colour, strokeWidth and strokeColor, of the text node. in this code, we are setting stroke color to – blue, text color to – brown and the stroke width to – 2.

Save this code in a file with the name StrokeExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window displaying the text with the specified stroke and color attributes as follows –

JavaFX - Text

Applying Decorations To Text

You may also apply decorations which includes strike through; wherein case a line is passed through the text. you may underline a text the use of the methods of the Text class.

You may strike through the text the usage of the technique setStrikethrough(). This accepts a Boolean value, pass the value true to this method to strike through the text as shown inside the following code box –

In the same way, you can underline a text by passing the value true to the method setUnderLine() as follows −

Example

The following program is an example demonstrating the way to follow decorations which includes underline or strike through to a text. save this code in a record with the name DecorationsExample.java.

Collect and execute the saved Java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window as shown below –

JavaFX - Text

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JavaFX Topics