?
Solved

New Line for text

Posted on 1997-11-14
11
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 80 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 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