It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Smilies in JTextPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Smilies in JTextPane" Watch "Smilies in JTextPane" New topic
Author

Smilies in JTextPane

marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Hi,

I am making a small chat program and I want my display area (a JTextPane component) to handle smiles. That is, for example, if it's requested to display the string " Hello : ) ", it should recognize the smiley shortcut and render it as an icon instead of plain text.

I am thinking of overriding the insertString method of the Document class. I will then anylise the new entry looking for smilies shortcuts on it in order to apply the appropriate style for them.

what do you guys think? Is there anything more simple?

Thanks in advance.
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
http://forums.sun.com/thread.jspa?threadID=5427306&tstart=0
Don't crosspost.

See my answer on sun's forums.

Regards,
Stas
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

Thank you Stanislav.

Marwen,
Please read this http://faq.javaranch.com/java/BeForthrightWhenCrossPostingToOtherSites


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
I was unaware of this, sorry.

Thanks Stanislav Lapitsky.
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Stanislav, I read the sample you provided. Basically there's a listener registered on the Pane's document that looks for smilies shortcuts once a new entry has been fired. As soon as it finds one, it schedules a new task to remove that shortcut from the document and insert and icon instead. This works fine, but how's that better than what I thought of? I find overriding the insertString method of the DefaultStyledDocument class more simple, no?
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
In simplest EditorKits insertString() is enough but you will have one more custom class.

Additionally there may be cases when insertString() isn't called but structure is updated e.g. when I call
protected void insert(int offset, ElementSpec[] data) method of DefaultStyledDocument.

Regards,
Stas
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
For now, I coded it my way and it works perfectly. However I did not consider updates resulting from a call to the insert method. So my question is would I be fine if I avoid calling this method (which I am not) or there may be times that it's called without me knowing?
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
If you use StyledEditorKit I see no big difference.

Regards,
Stas
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Smilies in JTextPane
 
Similar Threads
how to print the contents of jtextpane.
JSTL if-else
want to display word and excel sheets in JTEXTPANE
how to add find option
JTextPane Image icon HELP :S