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
Solved

New Line for text

Posted on 1997-11-14
11
197 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
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.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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 customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

837 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