Solved

New Line for text

Posted on 1997-11-14
11
203 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

696 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