Solved

Creating a white space

Posted on 2002-06-26
5
171 Views
Last Modified: 2010-03-31
Is there an existing class in Java wherein I can use just say create 50 spaces and start displaying a string in the text Area?

I don't like to create a method that just loop and create a space with append the string I want.
0
Comment
Question by:bubwit
5 Comments
 
LVL 5

Accepted Solution

by:
Calron earned 20 total points
ID: 7112844
You probably need this to set the initial size of the TextArea, instead of just filling in spaces, you can also use the methods: setColumns(int), be sure to read the API doc of the setColumns(int) and getColumnWidth() as setting the number of columns is not the same as allowing for n characters.

If you still want to use spaces, there is no ready made class, I would do the loop, though make sure you don't loop on a String (way too slow and creates a ton of temporary string objects), use StringBuffer:

StringBuffer s = new StringBuffer();
for (int i = 0; i < 50; i++) {
  s.append(" ");
}
String finalString = s.toString();


of course you can make this more efficient by adding 2 or more spaces at a time, its just supposed to be an example
0
 
LVL 92

Expert Comment

by:objects
ID: 7112929
This should be a bit faster:

char[] spaces = new char[50];
Arrays.fill(spaces, ' ');
String s = new String(spaces);
0
 

Expert Comment

by:udaykumar22
ID: 7115965
Hi,

I think this would be the fastest ;-)

String Str = new String("                                                 ");

Regards,
Uday.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8893423
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to Calron

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 85
firstswap challenge 20 68
Basic Java Case or If-Else statement... 3 43
Windows 10 IE Certificate Issue 10 42
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now