views:

107

answers:

2

Hi, I want to write a messenger application with java. I want to send smily pictures. I have written this code but it doesn’t work.

public class MyClient extends JFrame implements IClient {

JEditorPane editorPane=new JEditorPane();
final String SMILE = ClassLoader.getSystemClassLoader().getResource("images/1.gif").toString();

public void chat(String message) {
        try {
            StringBuffer  bfr= new StringBuffer(message);

            while(message.indexOf(":)") != -1) {
                int index = message.indexOf(":)");
                bfr.replace(index,index+2,"<IMG SRC=\""+SMILE +"\">");
                message= bfr.toString();

            }

try{
            editorPane.getEditorKit().read(new java.io.StringReader(message+"\n"),
                    editorPane.getDocument(), editorPane.getDocument().getLength());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    editorPane.setCaretPosition(editorPane.getDocument().getLength());
    }

… But it desnt show the smily in the editorpane, the output is : It is the address with which the smily has been saved. I cast the code like this:

(HTMLEditorKit)editorPane.getEditorKit().read(new java.io.StringReader(message+"\n"),
                    editorPane.getDocument(), editorPane.getDocument().getLength());

but it causes exception as this cast isn’t correct. So what I can I do? thank

A: 

I see 2 possibilities:

  1. you define a control string (like in msn) where :) becomes the smiley gif (for this one the receiver must have the gif too - so I'd go for an image folder that contains all gifs)
  2. you have to read the gif-file and transfer the content of the file to the receiver who then can display it (that's the more difficult solution)
Gambrinus
A: 

ok, i found the answer. i just assigned the HTMLEditorKit as my the editorkit of my JEditorPane

samuel