Example usage for javax.swing.text StyleConstants setIcon

List of usage examples for javax.swing.text StyleConstants setIcon

Introduction

In this page you can find the example usage for javax.swing.text StyleConstants setIcon.

Prototype

public static void setIcon(MutableAttributeSet a, Icon c) 

Source Link

Document

Sets the icon attribute.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextPane textPane = new JTextPane();
    StyledDocument doc = (StyledDocument) textPane.getDocument();

    Style style = doc.addStyle("StyleName", null);
    StyleConstants.setIcon(style, new ImageIcon("imagefile"));

    doc.insertString(doc.getLength(), "ignored text", style);
}

From source file:Main.java

public static final void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane textPane = new JTextPane();
    textPane.addMouseMotionListener(new MouseAdapter() {
        public void mouseMoved(MouseEvent e) {
            AttributeSet style = getAttributes(e);
            if (style != null && StyleConstants.getIcon(style) != null) {
                textPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            } else {
                textPane.setCursor(Cursor.getDefaultCursor());
            }//from w  w  w .  j a  va 2  s .co m
        }
    });
    frame.add(new JScrollPane(textPane));

    StyledDocument doc = (StyledDocument) textPane.getDocument();

    SimpleAttributeSet style = new SimpleAttributeSet();
    StyleConstants.setIcon(style, createImage());

    doc.insertString(doc.getLength(), "this is a test", null);
    doc.insertString(doc.getLength(), "test", style);
    doc.insertString(doc.getLength(), "this is a test\n", null);
    doc.insertString(doc.getLength(), "another image", style);

    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);

}

From source file:Main.java

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

    JTextPane textPane = new JTextPane();
    ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() {
        @Override/*from   ww w .jav  a 2  s. com*/
        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);
}

From source file:TextSamplerDemo.java

protected void addStylesToDocument(StyledDocument doc) {
    //Initialize some styles.
    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

    Style regular = doc.addStyle("regular", def);
    StyleConstants.setFontFamily(def, "SansSerif");

    Style s = doc.addStyle("italic", regular);
    StyleConstants.setItalic(s, true);

    s = doc.addStyle("bold", regular);
    StyleConstants.setBold(s, true);

    s = doc.addStyle("small", regular);
    StyleConstants.setFontSize(s, 10);

    s = doc.addStyle("large", regular);
    StyleConstants.setFontSize(s, 16);

    s = doc.addStyle("icon", regular);
    StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
    ImageIcon pigIcon = createImageIcon("images/Pig.gif", "a cute pig");
    if (pigIcon != null) {
        StyleConstants.setIcon(s, pigIcon);
    }/*w w w . j a  v  a2 s  .  c o m*/

    s = doc.addStyle("button", regular);
    StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
    ImageIcon soundIcon = createImageIcon("images/sound.gif", "sound icon");
    JButton button = new JButton();
    if (soundIcon != null) {
        button.setIcon(soundIcon);
    } else {
        button.setText("BEEP");
    }
    button.setCursor(Cursor.getDefaultCursor());
    button.setMargin(new Insets(0, 0, 0, 0));
    button.setActionCommand(buttonString);
    button.addActionListener(this);
    StyleConstants.setComponent(s, button);
}

From source file:org.drugis.addis.gui.wizard.AddStudyWizard.java

public static void addStylesToDoc(StyledDocument doc) {
    //Initialize some styles.
    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

    Style regular = doc.addStyle("regular", def);

    Style bold = doc.addStyle("bold", regular);
    StyleConstants.setBold(bold, true);

    // The image must first be wrapped in a style
    Style style = doc.addStyle("tip", null);
    StyleConstants.setIcon(style, Main.IMAGELOADER.getIcon(FileNames.ICON_TIP));
}