Well, I'm doing pretty good with my WebChat application, but I was thinking to add some smileys!

At first, I would like the app to auto detect the smileys in standard form from a given string and place the image in its place, but I came across the problem: how can I add images to a specific/moving place in a jtextarea?

Also, I can easily write something that will "look for" something like ":)", but then, how would I tell the jtextarea to place a smiley there instead (maybe add 1 word at a time with jtextarea.append(word)?)

In that case, I would need to do an append like function on an image?

Not sure if its possible, but I'm throwing it out there.. lol

Dani AI

Generated

— Appending raw HTML into a JTextArea will just show the tags because JTextArea is a plain-text component. Use a text component that supports styled content or HTML instead. (docs.oracle.com)

A practical approach for smileys is to switch to JTextPane (or JEditorPane for full HTML) and insert icons where the emoticon text appears. JTextPane has a convenience method and the styled-document API that let you embed an Icon or a Swing Component inline. Example patterns (minimal):

JTextPane pane = new JTextPane();
StyledDocument doc = pane.getStyledDocument();

// quick convenience: insert at caret
pane.insertIcon(new ImageIcon(getClass().getResource("/smiley.png")));

// or insert at a specific position using a Style
Style s = pane.addStyle("smile", null);
StyleConstants.setIcon(s, new ImageIcon(getClass().getResource("/smiley.png")));
doc.insertString(pos, " ", s);

This uses the JTextPane convenience method or StyleConstants.setIcon on a Style and then inserts a single-character placeholder styled with that icon. Both approaches are supported by Swing text APIs. (docs.oracle.com)

To auto-replace as users type: attach a DocumentFilter or DocumentListener, detect the emoticon pattern, then on the EDT remove the text range and insert the icon-style at that offset. Remember: document changes must happen on the Event Dispatch Thread, inserted icons occupy one document character (so update caret/selection accordingly), and scale your icon to the text line height for consistent layout. For full-message HTML rendering instead (if you prefer building HTML fragments with <img> tags), use JEditorPane with content type "text/html", but that requires managing images/URLs and sanitizing input. (docs.oracle.com)

References:

I came across this post: http://www.daniweb.com/forums/thread210869.html
Which said many Swing components can take html tags.

I'm not too great at the whole HTML thing, but although JTextAreas are swing components, I do not believe they fall into this category.

here is what I've tried regarding HTML:

import java.awt.Dimension;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

class test extends JFrame {
	
	JTextArea area;
	Scanner scan;
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new test();
			}
		});
	}
	
	public test() {
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	
		scan = new Scanner(System.in);
		area = new JTextArea();
		JScrollPane sp = new JScrollPane(area);
		sp.setPreferredSize(new Dimension(300,300));
		this.add(sp);
		
		this.pack();
		this.setVisible(true);
		area.append(replaceSmileys(":)"));
	}
	
	public String replaceSmileys(String line) {
		if (line.contains(":)"))
			return line.replace(":)", "<html><b><u>T</u>est</b></html>");
		else 
			return line;
	}
}

That should have replaced ":)" with Test, instead it just kept the syntax. I believe this would work with JLabels? I'm just hoping theres a better way so I dont reinvent the JTextArea.. lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.