Reputation: 427
I'm trying to add a large body of text into a String. For this, I need to put in a new line within the text so that when it prints, everything won't be in a straight line. How can I put in a new line within a string text?
This is my code:
String writing = "7 Dear friends, let us love one another, for love comes from God. Everyone who loves has been born of God and knows God. 8 Whoever does not love does not know God, because God is love. 9 This is how God showed his love among us: He sent his one and only Son into the world that we might live through him. 10 This is love: not that we loved God, but that he loved us and sent his Son as an atoning sacrifice for our sins. 11 Dear friends, since God so loved us, we also ought to love one another. 12 No one has ever seen God; but if we love one another, God lives in us and his love is made complete in us. 1 Everyone who believes that Jesus is the Christ is born of God, and everyone who loves the father loves his child as well. 2 This is how we know that we love the children of God: by loving God and carrying out his commands. 3 In fact, this is love for God: to keep his commands. And his commands are not burdensome, 4 for everyone born of God overcomes the world. This is the victory that has overcome the world, even our faith. 5 Who is it that overcomes the world? Only the one who believes that Jesus is the Son of God.";
JTextArea sampleWriting = new JTextArea();
sampleWriting.setText(writing);
sampleWriting.setFocusable(false);
JTextField userTypingRegion = new JTextField();
dataCollectionRegion.add(sampleWriting);
dataCollectionRegion.add(userTypingRegion);
I tried /n but in the JTextArea, the writing is all in a single line.
Upvotes: 1
Views: 233
Reputation: 236004
Try this:
StringBuilder sb = new StringBuilder("long string");
// adds a line separator
sb.append(System.getProperty("line. separator"));
sb.append("another long string");
// ... more lines ...
String result = sb.toString();
The good part of the above code is that the line separator added is the correct one for the platform you're in (Linux, Windows, etc.)
EDIT :
Now that you've posted the code, I can see that is not such a long string after all :) . It's very simple then, try this:
String writing = "Hello. \nWorld. \nLamp.";
Upvotes: 6