Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Dimension;
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 Main {

    static ImageIcon anImage;
    static final String imageToken = ":)";
    String[] imageTokens = { imageToken };

    private void initComponents() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextPane textPane = new JTextPane();
        ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(final DocumentEvent de) {
                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 : imageTokens) {
                                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) {
                                        case imageToken:
                                            StyleConstants.setIcon(attrs, anImage);
                                            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) throws Exception {
        anImage = new ImageIcon(ImageIO.read(new URL("http://www.java2s.com/style/download.png")));
        new Main().initComponents();
    }
}