Label and Text differences in JavaFX
Asked Answered
M

3

68

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.

Marseillaise answered 23/6, 2014 at 20:35 Comment(2)
non-editable in this context means you can't edit it on screen, it doesn't mean you can't edit it programmatically.Deduce
One difference is the position in a group. When you add a Text it is placed above the group and Label is placed inside...Marseillaise
H
89

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.

Hehre answered 24/6, 2014 at 14:45 Comment(0)
P
17

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.

Protonema answered 26/5, 2015 at 15:43 Comment(1)
And you can use a Text clipping, giving the node shape by the text.Longing
A
-2

Labels

  • A label displays text or image which the user cannot directly change or edit . *specify the image for the label.

TextFields

  • A TextField displays text or image which the user can directly change or edit ..
  • don't specify image for the text field
Allcot answered 27/7, 2018 at 23:15 Comment(1)
I think the question refers only to the component Text and not to TextField.Crider

© 2022 - 2024 — McMap. All rights reserved.