Solved

New Line for text

Posted on 1997-11-14
11
190 Views
Last Modified: 2010-04-16
I am trying to place some text in a applet (e.g., Text = "this is the text") I want to make the text goto the a new line.  I know that I need to use \n, but how do I put it in the string to make it brake to a new line.
0
Comment
Question by:Maurice072297
11 Comments
 
LVL 4

Expert Comment

by:russgold
ID: 1230620
String text = "this is line 1" + '\n' + "this is line 2";
0
 
LVL 4

Expert Comment

by:russgold
ID: 1230621
Of course, that technique is not portable.  You do better to use something like:

String separator = System.getProperty( "line.separator" );
String text = "this is line 1" + separator + "this is line 2";

which is guaranteed to be portable to all platforms.
0
 

Author Comment

by:Maurice072297
ID: 1230622
This will not work for applets??  only applications correct??
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Expert Comment

by:russgold
ID: 1230623
No, this works for applets, as well.  It is one of the properties that *is* accessible from an applet.
0
 

Author Comment

by:Maurice072297
ID: 1230624
When I use this statement I either get a box or I get a seperator line.  It does not place the second string on a new line.
0
 
LVL 4

Expert Comment

by:russgold
ID: 1230625
Which statement are you using?  Is your applet on the web somewhere for me to try?
0
 

Author Comment

by:Maurice072297
ID: 1230626
I have not put it on the web yet, cause I can't get it to work.  I tried the statement that you gave me, an I also tried

String text;

public void init() {
  text = "This is string 1" + '\n' + "This is string 2"
}
public void paint(Graphics g) {
  g.drawString(text, 20, 50);
}

neither statement works.  both either give me a | or a box.

0
 

Expert Comment

by:maha
ID: 1230627
instead of "\n" try "\r\n"


Maha
0
 

Author Comment

by:Maurice072297
ID: 1230628
That did not work either.
0
 

Accepted Solution

by:
rans earned 20 total points
ID: 1230629
Did you try to use Label instead of direct drawing to screen ?

Label l=new Label("line1 \n line2");
add(l);
0
 

Author Comment

by:Maurice072297
ID: 1230630
I got it to work.  I used a Texarea.  But the way that you suggested also works.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
ForLoop Example 3 49
Maven Project: Hibernate Dependencies Conflict 10 24
ejb on wildfly 5 20
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question