• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

New Line for text

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
Maurice072297
Asked:
Maurice072297
1 Solution
 
russgoldCommented:
String text = "this is line 1" + '\n' + "this is line 2";
0
 
russgoldCommented:
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
 
Maurice072297Author Commented:
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!

 
russgoldCommented:
No, this works for applets, as well.  It is one of the properties that *is* accessible from an applet.
0
 
Maurice072297Author Commented:
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
 
russgoldCommented:
Which statement are you using?  Is your applet on the web somewhere for me to try?
0
 
Maurice072297Author Commented:
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
 
mahaCommented:
instead of "\n" try "\r\n"


Maha
0
 
Maurice072297Author Commented:
That did not work either.
0
 
ransCommented:
Did you try to use Label instead of direct drawing to screen ?

Label l=new Label("line1 \n line2");
add(l);
0
 
Maurice072297Author Commented:
I got it to work.  I used a Texarea.  But the way that you suggested also works.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now