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 :-)
The correct way to do things is using things made for the things you need. If you want a line break (enter), use <br> ; If you want to define a paragraph, use <p> . Show activity on this post. According to this, the <br> element is used to insert a line break without starting a new paragraph.
The HTML <textarea> tag is used to specify a multiline input control text area in HTML5. The <cols> and <rows> attributes specify size of a textarea.
The following code example uses TextBox, a derived class, to create a multiline TextBox control with vertical scroll bars. This example also uses the AcceptsTab, AcceptsReturn, and WordWrap properties to make the multiline text box control useful for creating text documents.
To create a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows. Specifies that on page load the text area should automatically get focus.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With