44

I need to display a multiline, read-only text - which control can be used for that? It should only display the text, like a Label does, however Label does not support multiline?

Thanks for any hint :-)

Seyit Bilal
  • 191
  • 2
  • 16
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • 1
    Did you look at the [TextArea](http://docs.oracle.com/javafx/2/api/javafx/scene/control/TextArea.html) control? You can make it non-editable and style it to look like a label. – OttPrime Apr 12 '13 at 18:29
  • Thank you OttPrime, yes, that's the idea I am currently following. Still I am wondering if there is no real control for displaying text? Something like a RichTextBox in .NET? – stefan.at.kotlin Apr 12 '13 at 18:47
  • The [HTMLEditor](http://docs.oracle.com/javafx/2/ui_controls/editor.htm) is the closest you'll get to a RichTextBox but it's probably a bit more functionality than you're after. – OttPrime Apr 12 '13 at 19:14

5 Answers5

83

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.

If the label has wrapText set to true and there is not enough space to display a single line of text, then the label will wrap the text to a new line.


If you want text in different styles, then, if using JavaFX 2, place multiple labels in a FlowPane or, if you are using Java 8+, place the labels in a TextFlow component.


lovemenot

Produced by the following code:

import javafx.scene.Scene;

import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.scene.image.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class LoveMeNot extends Application {

  public static void main(String[] args) throws Exception { launch(args); }

  @Override public void start(final Stage stage) throws Exception {
    Label label = new Label(WORDS);
    label.setWrapText(true);
    label.setStyle("-fx-font-family: \"Comic Sans MS\"; -fx-font-size: 20; -fx-text-fill: darkred;");

    ImageView image = new ImageView(IMAGE);
    image.setOpacity(0.3);

    StackPane layout = new StackPane();
    layout.setStyle("-fx-background-color: mistyrose; -fx-padding: 10;");
    layout.getChildren().setAll(image, label);

    stage.setTitle("Love Me Not");
    stage.setScene(new Scene(layout));
    stage.show();
  }

  // creates a triangle.
  private static final String WORDS = 
    "Love not me for comely grace,\n" +
    "For my pleasing eye or face,\n" +
    "Nor for any outward part,\n" +
    "No, nor for my constant heart,\n" +
    "For these may fail, or turn to ill.\n" +
    "So thou and I must sever.\n" +
    "Keep therefore a true woman’s eye,\n" +
    "And love me still, but know not why,\n" +
    "So hast thou the same reason still\n" +
    "To doat upon me ever.";

  private static final Image IMAGE = 
    new Image("http://icons.iconarchive.com/icons/artdesigner/gentle-romantic/256/rose-icon.png");
}

Try running the above program and resizing the window to see the effect of the \n new line values in the label's text as well as the wrapText property on the label. Vary the wrapText setting from true to false to see the difference between having wrapText switched on and off.

Neuron
  • 5,141
  • 5
  • 38
  • 59
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • 31
    Oh god, Comic Sans MS is awful. – randers Jan 03 '16 at 11:04
  • 11
    lol, truly rAnders, somewhere Microsoft Bob is hugging me and a font designer is cursing me :-) – jewelsea Jan 04 '16 at 18:45
  • 1
    everything about your sample image makes me shiver – Neuron Jul 11 '17 at 16:40
  • 1
    That got an upvote for me for being one of the few responses that made me laugh :D – Neil Benn Aug 17 '17 at 12:30
  • didn't work for me, from the \n onwards it just displays "..." – golimar Jan 29 '21 at 06:25
  • 1
    @golimar Label text will be elided if there is not enough layout space to display the full text. Your issue is with the constraints you have set on your layout pane and it’s contents. If you continue to have an issue. create a new question which includes a [mcve]. In your new question you can reference this answer. – jewelsea Jan 29 '21 at 20:08
10

If you set a max width you want for your Label, then you set setWrapText to true so it displays the text multiline:

Label label = new Label("Your long text here");
label.setMaxWidth(180);
label.setWrapText(true);
Neuron
  • 5,141
  • 5
  • 38
  • 59
Issam El omri
  • 111
  • 1
  • 5
9

You can also use Text to appear in multiline, by setting wrappingWidthProperty according to your needs.

Text text = new Text();
text.wrappingWidthProperty().set(345);

In this code, I have set max width to 345.0 ,So When the text's size reaches beyond 345 pixels will be wrapped to next Line.

Neuron
  • 5,141
  • 5
  • 38
  • 59
guru_001
  • 535
  • 6
  • 12
6

If the text has \n (newline) characters in it, then the label will place wrap to a new line wherever the newline character is.

It not works when you set text from FXML file as well as from visual FXML editor.

Steel Rat
  • 101
  • 1
  • 3
  • 9
    Since it's "XML" you have to use this in FXML: Not sure if Scene Builder lets you do that, might need to edit the FXML directly. – User Apr 14 '17 at 17:44
  • @Manius Where can I find a list of ` `? Tried googling `FXML escape codes` but could not find any list. Thanks. – jpllosa May 17 '17 at 04:22
  • The numbers are just ASCII values! http://www.asciitable.com/ (13 = return) :) Also, for some controls (I forget which, a bit past the Ballmer Peak atm) Scene Builder has a "multi line mode" that you can enable in the UI if you look carefully. – User May 21 '17 at 00:40
1

Text class also available. A good explanation in this answer label-and-text-differences-in-javafx Basically use Text for where you're not getting input, otherwise Label is better.

Community
  • 1
  • 1
SystemsInCode
  • 629
  • 7
  • 19