Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:22:17+00:00 2026-06-15T11:22:17+00:00

I’ve been wondering if for example: JTextPane chatTextArea = new JTextPane(); s.replaceAll(:\\), emoticon()); public

  • 0

I’ve been wondering if for example:

    JTextPane chatTextArea = new JTextPane();
    s.replaceAll(":\\)", emoticon());

    public String emoticon(){
           chatTextArea.insertIcon(new ImageIcon(ChatFrame.class.getResource("/smile.png")));
           return "`";
       }

can put a picture and a “`” everywhere “:)” is found. When I run it like this if s contains a “:)” then the whole s gets replaced just by the icon.
Is there a way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:22:18+00:00Added an answer on June 15, 2026 at 11:22 am

    Here is a small example I made (+1 to @StanislavL for the original), simply uses DocumentListener and checks when a matching sequence for an emoticon is entered and replaces it with appropriate image:

    enter image description here

    NB: SPACE must be pressed or another character/emoticon typed to show image

    import java.awt.Dimension;
    import java.awt.Image;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.SwingUtilities;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.AbstractDocument;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;
    import javax.swing.text.Utilities;
    
    public class JTextPaneWithEmoticon {
    
        private JFrame frame;
        private JTextPane textPane;
        static ImageIcon smiley, sad;
        static final String SMILEY_EMOTICON = ":)", SAD_EMOTICON = ":(";
        String[] emoticons = {SMILEY_EMOTICON, SAD_EMOTICON};
    
        private void initComponents() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            textPane = new JTextPane();
    
            //add docuemntlistener to check for emoticon insert i.e :)
            ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() {
                @Override
                public void insertUpdate(final DocumentEvent de) {
                    //We should surround our code with SwingUtilities.invokeLater() because we cannot change document during mutation intercepted in the listener.
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            try {
                                StyledDocument doc = (StyledDocument) de.getDocument();
                                int start = Utilities.getRowStart(textPane, Math.max(0, de.getOffset() - 1));
                                int end = Utilities.getWordStart(textPane, de.getOffset() + de.getLength());
    
                                String text = doc.getText(start, end - start);
    
                                for (String emoticon : emoticons) {//for each emoticon
    
                                    int i = text.indexOf(emoticon);
                                    while (i >= 0) {
                                        final SimpleAttributeSet attrs = new SimpleAttributeSet(doc.getCharacterElement(start + i).getAttributes());
                                        if (StyleConstants.getIcon(attrs) == null) {
    
                                            switch (emoticon) {//check which emtoticon picture to apply
                                                case SMILEY_EMOTICON:
                                                    StyleConstants.setIcon(attrs, smiley);
                                                    break;
                                                case SAD_EMOTICON:
                                                    StyleConstants.setIcon(attrs, sad);
                                                    break;
                                            }
    
                                            doc.remove(start + i, emoticon.length());
                                            doc.insertString(start + i, emoticon, attrs);
                                        }
                                        i = text.indexOf(emoticon, i + emoticon.length());
                                    }
                                }
                            } catch (BadLocationException ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
                }
    
                @Override
                public void removeUpdate(DocumentEvent e) {
                }
    
                @Override
                public void changedUpdate(DocumentEvent e) {
                }
            });
    
            JScrollPane scrollPane = new JScrollPane(textPane);
            scrollPane.setPreferredSize(new Dimension(300, 300));
    
            frame.add(scrollPane);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
    
            try {//attempt to get icon for emoticons
                smiley = new ImageIcon(ImageIO.read(new URL("http://facelets.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/e/m/emoticons0001.png")).getScaledInstance(24, 24, Image.SCALE_SMOOTH));
                sad = new ImageIcon(ImageIO.read(new URL("http://zambia.primaryblogger.co.uk/files/2012/04/sad.jpg")).getScaledInstance(24, 24, Image.SCALE_SMOOTH));
    
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new JTextPaneWithEmoticon().initComponents();
                }
            });
        }
    }
    

    References:

    • How to add smileys in java swing?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
i got an object with contents of html markup in it, for example: string
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.