i have created a chat program and i want to add smiley to it......i know how to add smiley in a program,but my logic works for single smiley in a message.......want logic for multiple smiley in prgm,,,,

int zi=msg.indexOf(':');

    if(zi!=-1)
    {  

      try{
      st1=msg.substring(msg.indexOf(':'),msg.indexOf(':')+2);
     String msg_start=msg.substring(1,msg.indexOf(':'));

     int msg_end_index=msg.indexOf(':')+3;

     if(msg_end_index>msg.length())
           msg_end="";

       else
      msg_end=msg.substring(msg_end_index);


   try {
      ch.document.insertString(ch.document.getLength(), "\n"+msg_start, ch.attributes);
    } catch (BadLocationException badLocationException) {System.err.println("Oops"); }


   Style labelStyle=ch.context.getStyle(StyleContext.DEFAULT_STYLE);

   if(st1.equals(":)"))
   {st1="D:/sim2";

   Icon icon=new ImageIcon(st1+".png");
   JLabel label=new JLabel(icon);
   StyleConstants.setComponent(labelStyle,label);

   //insert smiley
   try
   {
       ch.document.insertString(ch.document.getLength(), "Ignored", labelStyle);
   }catch(Exception e ){}
   }


   if(msg_end!=null)
   {  //end
   try {
      ch.document.insertString(ch.document.getLength(), msg_end, ch.attributes);
    } catch (BadLocationException badLocationException) {System.err.println("Oops"); }

   }



    }catch(Exception e){e.printStackTrace();}

    }



    else 
   {    //insert message
       try {
      ch.document.insertString(ch.document.getLength(), "\n"+msg.substring(1), ch.attributes);
    } catch (BadLocationException badLocationException) {System.err.println("Oops"); }
   }

first of all, don't check for ':' what if someone has the text
"this is my remark: checking for that char on it's own, will be treated twice in your if, while it should only be treated once :)"

just jump for each and every occurence of your smiley (:), for example) not just for the first one. there are several ways to do this, using substrings is (just) one of them

This article has been dead for over six months. Start a new discussion instead.