Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JEditorPane replace text with an image

843805Oct 9 2006 — edited Oct 9 2006
hello,
please before you all sigh adn start ranting 'search forums first!!' let me just be clear..

ive search the forums, and google and any other place icould imagin finding java code, and although ive found many similar examples i cant find a way that i can integrate such techniques as styledDocuments and such to my specific problem.

im creating an IM for an intranet, i type text into one JTextArea, and when i click send i have the text sent over teh network and appear on the JEditorPane, but i need certain strings replaced by an emoticon, much like every other IM.

below i will post the code im using to parse the code to html formatted code and if someone could please help me integrate a way to replace certaing strings with images BY RELATIVE PATHS!!!
	/**
	 * This method initializes txtConv	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */
	private JEditorPane getTxtConv() {
		if (txtConv == null) {
			txtConv = new JEditorPane();
			txtConv.setContentType("text/html");
			txtConv.setPreferredSize(new Dimension(344, 346));
			txtConv.setText("<html><body><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>Start Conversation<br><br></td></tr></table></body></html>");
			txtConv.setToolTipText("<html>Messages that have been send and <br>received.</html>");
			txtConv.setEditable(false);
		}
		return txtConv;
	}
...
	private void initializeEmoticons()
	{
		Emoticons = new HashMap<String, String>();
		Emoticons.put(":p", "/gui/smileys/cheeky.png");
		Emoticons.put(":P", "/gui/smileys/cheeky.png");
		Emoticons.put(":)", "/gui/smileys/happy.png");
		Emoticons.put(":(", "/gui/smileys/sad.png");
		Emoticons.put(":$", "/gui/smileys/blush.png");
		Emoticons.put(":s", "/gui/smileys/confused.png");
		Emoticons.put(":S", "/gui/smileys/confused.png");
		Emoticons.put(":'(", "/gui/smileys/crying.gif");
		Emoticons.put(":o", "/gui/smileys/omg.png");
		Emoticons.put(":O", "/gui/smileys/omg.png");
		Emoticons.put(";(", "/gui/smileys/wink.gif");
		Emoticons.put(":d", "/gui/smileys/yay.png");
		Emoticons.put(":D", "/gui/smileys/yay.png");
		Emoticons.put("(a)", "/gui/smileys/angel.png");
		Emoticons.put("(A)", "/gui/smileys/angel.png");
		
	}
...
	/**
	 * 
	 * @param s - unformatted string
	 * @param type - 0 -> from me, 1 -> from other
	 */
	public void addMessage(String s, int type)
	{
		String[] body;
		String[] conv = this.getTxtConv().getText().split("</body>");
		String displayName = "";
		
		if(type == 0)
		{
			body = s.split("\r\n");
			displayName = this.contacts[0].getDisplayName();
		}else
		{
			String[] parts = s.split("\r\n\r\n");
			String[] headers = parts[0].split("\r\n");
			body = parts[1].split("\r\n");
			
			displayName = (headers[0].substring(headers[0].indexOf(" ", 5), headers[0].lastIndexOf(" "))).replace("%20", " ");
		}
		
		conv[0] += "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td colspan=\"2\">"
					+ displayName + " says:</td></tr><tr><td>����</td><td width=\"100%\">";
		 
		for(int i = 0; i < body.length; i++)
		{
			Object[] EmoticonSet = Emoticons.keySet().toArray(); 
			for(int j = 0; j < EmoticonSet.length; j++)
			{
				if(body.contains(EmoticonSet[j].toString()))
{
// some how replace string with image
}
}

if(i == 0)
{
conv[0] += body[i];
}else
{
conv[0] += "<br>" + body[i];
}
}
this.getTxtConv().setText(conv[0] + "</td></tr></table></body>" + conv[1]);
Document doc = this.getTxtConv().getDocument();
System.out.println(doc.toString());
}



an example of a String to be formatted is:
header line 1\r\n
header line 2\r\n
\r\n
body line 1\r\n
body line 2 ... :P...\r\n
body line 3
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2006
Added on Oct 9 2006
0 comments
32 views