GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes printing TABS in strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "printing TABS in strings" Watch "printing TABS in strings" New topic
Author

printing TABS in strings

Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
I am boggled with this, being new to Java and all. Why is it that the following code will not allow me to print TABS in strings? (i.e., tabs="testing\tone\ttwo\tthree"
Here is my code:


if you don't know, then ask. if you do know, then share. love is knowledge.
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
I should have added that when I run that code (using Java 1.4.1_01 on the Debian operating system), it just prints out that string with all the words crammed together, inside the box. This is wierd.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It seems like the drawString() method doesn't know how to deal with special characters, such as tab. I would suggest you split the String up and use separate drawString() calls with the appropriate x and y arguments to position them where you want.


Java API Documentation
The Java Tutorial
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
Thanks
Surely there is some other way to do this? Using separate drawString() methods is rather inefficient, isn't it?
The Deitel & Deitel book just mentioned the "\t" character, but didn't give an example using it. Now I know why, considering that the character was mentioned in Chapter 2, I believe.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Well, if you are doing a text-based program, the \t character can be quite useful. It will print as expected if you do something like
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
Correct, but that isn't what I want. I want to do this within an applet. Can it just not be done without sprinkling drawString() methods for every word to be tabbed?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Perhaps someone over in The Swing / JFC / AWT Forum has an explanation...
[ January 29, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
 
subject: printing TABS in strings