File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Word wrapping in Canvas !!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Word wrapping in Canvas !!!!" Watch "Word wrapping in Canvas !!!!" New topic
Author

Word wrapping in Canvas !!!!

Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Hi Friends,
I have a canvas in a chat applet where all the messages are being drawn/displayed send by different users. Now, the problem is - the width of a canvas is fixed. Now, the problem is when the user types a message the length of which is more than the canvas width, the message gets cuts when the canvas width exceeds. I want the canvas when the width of message exceeds, the rest of the message should be displayed on the next line. Also, I want the word wrapping feature ?? What should I do ?
I hope there is of course someone to help me !!
Regards and thanx in advance.


Best wishes,
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
Figure out how many characters will fit on the width of your canvas (using FontMetrics would be easiest), then print that many characters on each line.
Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Hi,
I'm sorry as I really dont know how to use FontMetrics class. Can u give me some guidance ???
Thanx in advance.
Regards,
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
Here is a link to the FontMetrics docs:
http://java.sun.com/j2se/1.3/docs/api/java/awt/FontMetrics.html
You can get a FontMetrics object by calling the method getFontMetrics(Font font) on your canvas. There are functions in the class that return values for the height and width of characters or strings for that font.
For example, to find the width of a string in Arial 12pt plain text:

Vagisha Sharma
Greenhorn

Joined: Mar 01, 2001
Posts: 12
Hi Sam,
Can't you use a TextArea instead of a Canvas? That should solve your problem.

-Vagisha
Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Hi Vagisha,
Sorry dear !!, As I have already done it using Canvas !! Conversion of canvas to textarea would be a long process for me.
Thanks Angela for your guidance !!
Regards,
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
TextArea doesn't have word wrapping and can't display different fonts and colors, so that wouldn't work anyway.
Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Hi Angela,
I think word wrapping in textarea is possible if you remove the horizontal scrollbar. But yes, writing in diffrent colors, I assume would be a problem. But then you can always change the Foreground color of a text you are writing. But if you have to draw an image on a textarea, I dont know how will you ? Many chat applets are doing this including yahoo.com and rediff.com.
If possible , do tell me what they are using , a textarea or a canvas, because they are drawing images also. I doubt if images can be drawn on a textarea.
Regards and thanks,
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
You are right about the word wrapping, my mistake.
I can't say for sure what Yahoo uses for their chat applet, since I haven't seen the source code, but I'm pretty sure it must be a custom Canvas component. TextArea can't draw images and will only show text in one font and color. And if they were using JTextArea, which does support images and different fonts and colors, then the applet would require the JRE plugin.
Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Yes Angela,
You're right ! Canvas would the right component to use.
Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Word wrapping in Canvas !!!!