We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

New Line for text

Maurice072297
on
Medium Priority
231 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.
Comment
Watch Question

Commented:
String text = "this is line 1" + '\n' + "this is line 2";

Commented:
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.

Author

Commented:
This will not work for applets??  only applications correct??

Commented:
No, this works for applets, as well.  It is one of the properties that *is* accessible from an applet.

Author

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.

Commented:
Which statement are you using?  Is your applet on the web somewhere for me to try?

Author

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.

Commented:
instead of "\n" try "\r\n"


Maha

Author

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I got it to work.  I used a Texarea.  But the way that you suggested also works.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.