The most intelligent Java IDE
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Swing / AWT / SWT / JFace
 
RSS feed
 
New topic
Author

Smilies in JTextPane

marwen Bakkar
Greenhorn

Joined: Jan 28, 2010
Messages: 24

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
Greenhorn

Joined: Dec 01, 2009
Messages: 21

http://forums.sun.com/thread.jspa?threadID=5427306&tstart=0
Don't crosspost.

See my answer on sun's forums.

Regards,
Stas
Maneesh Godbole
Bartender

Joined: Jul 26, 2007
Messages: 4241

Thank you Stanislav.

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

work is the scourge of the drinking class
marwen Bakkar
Greenhorn

Joined: Jan 28, 2010
Messages: 24

I was unaware of this, sorry.

Thanks Stanislav Lapitsky.
marwen Bakkar
Greenhorn

Joined: Jan 28, 2010
Messages: 24

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?

This message was edited 2 times. Last update was at by marwen Bakkar

Stanislav Lapitsky
Greenhorn

Joined: Dec 01, 2009
Messages: 21

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
Greenhorn

Joined: Jan 28, 2010
Messages: 24

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
Greenhorn

Joined: Dec 01, 2009
Messages: 21

If you use StyledEditorKit I see no big difference.

Regards,
Stas
marwen Bakkar
Greenhorn

Joined: Jan 28, 2010
Messages: 24

Thanks.
 
jQuery in Action
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Swing / AWT / SWT / JFace
 
RSS feed
 
New topic
hibernate profiler