Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label and Text differences in JavaFX

Tags:

java

javafx

fxml

What is the difference between javafx.scene.text.Text and javafx.scene.control.Label?

The documentation says:

  • Label is a non-editable text control.
  • The Text class defines a node that displays a text.

But the Label class has a method "setText" and "textProperty", therefore is editable.

like image 408
multiplayer1080 Avatar asked Jun 23 '14 20:06

multiplayer1080


People also ask

What is a Label in JavaFX?

Label is a part of JavaFX package . Label is used to display a short text or an image, it is a non-editable text control. It is useful for displaying text that is required to fit within a specific space, and thus may need to use an ellipsis or truncation to size the string to fit.

How do you change the text of a JavaFX Label?

Here is an example of setting the font of a JavaFX Label : Label label = new Label("A label with custom font set."); label. setFont(new Font("Arial", 24));

Is Label a node JavaFX?

Method DetailA Label can act as a label for a different Control or Node. This is used for Mnemonics and Accelerator parsing.

Can you display multiple lines of text in a Label JavaFX?

You can display multi-line read-only text in a Label . If the text has \n (newline) characters in it, then the label will wrap to a new line wherever the newline character is.


Video Answer


2 Answers

As Harry Blargle pointed out, "non-editable" means "not editable by the user." So both Label and Text are non-editable.

Label and Text have different CSS properties. Label inherits from Labeled, Control, and Region, which means it inherits a great many styleable properties which Text doesn't have. A Label can have alignment, a graphic, a background, a border, a displayed keyboard mnemonic, built-in wrapping, and can be intelligently clipped with an ellipsis ("…").

Text can participate in a TextFlow. (Technically, Label can also, but it is treated as just another embedded control and is not laid out as text.)

In general, if you want to show the purpose of an input control by placing one or more words next to it, and/or you want to allow direct keyboard navigation to an input control, you use a Label. If you want to display text content not associated with input, you use Text.

like image 152
VGR Avatar answered Oct 11 '22 13:10

VGR


A Text is a geometric shape (like a Rectangle or a Circle), while Label is a UI control (like a Button or a CheckBox).

In Swing, geometric shapes were restricted to the painting mechanism, while in JavaFX they can be used in more generic ways.

like image 16
Jan Bodnar Avatar answered Oct 11 '22 13:10

Jan Bodnar