使用pane.getDocument.insert()在JEditorPane中插入图像 [英] Inserting images in JEditorPane using pane.getDocument.insert()

查看:97
本文介绍了使用pane.getDocument.insert()在JEditorPane中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个聊天应用程序,我想将字符串追加到JEditorPane中,所以我使用JEditorPane.getDocument.insert()方法来执行此操作:

I am creating a chat application and I want to append strings into JEditorPane, so I am using JEditorPane.getDocument.insert() method to do this:

clientListDoc.insertString(clientListDoc.getLength(),image+"-"+name[0]+"\n", null);

但现在我也想显示图像。我已将内容类型设置为HTML并使用此:

But now I also want to display images. I have set the content type to HTML and I use this:

String temp=ClassLoader.getSystemResource("images/away.png").toString();
image="<img src='"+temp+"'></img>";

但是如果我使用insert()但是当我使用setText时,我没有在JEditorPane上获取图像( )显示图像。请帮忙!!我想做这两件事!

But I don't get images on JEditorPane if I use insert() but when I use setText() images are displayed. Please help!! I want to do both these things!

一种方法我可以使用getText来获取前面的字符串并将新字符串附加到此字符串然后使用setText( )设置整个字符串,但有更好的解决方案吗?

One approach I though could be to use getText to get the previous strings and append the new string to this string and then use setText() to set the entire string but is there a better solution?

推荐答案

使用 setText()方法,它正在为您格式化为HTML。使用 insertString ,您的标记将转换为文本。查看文档的源HTML,您会看到< img src = imagepath> 将是& lt; img src = imagepath& gt;

With the setText() method, it is being formated to HTML for you. With insertString, your markups are converted to text. Look at the source HTML of your document, you'll see that < img src=imagepath > will be & lt;img src=imagepath & gt;.

您需要使用HTMLDocument类正确插入图片:

You'll need to use HTMLDocument class to insert your image properly:

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

class Test {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JEditorPane edPane = new JEditorPane(); 

        try {

            edPane.setContentType("text/html");

            System.out.println(edPane.getText());

            HTMLEditorKit hek = new HTMLEditorKit();

            edPane.setEditorKit(hek);

            HTMLDocument doc = (HTMLDocument) edPane.getDocument();

            doc.insertString(0, "Test testing", null);

            Element[] roots = doc.getRootElements();
            Element body = null;
            for( int i = 0; i < roots[0].getElementCount(); i++ ) {
                Element element = roots[0].getElement( i );
                if( element.getAttributes().getAttribute( StyleConstants.NameAttribute ) == HTML.Tag.BODY ) {
                    body = element;
                    break;
                }
            }

            doc.insertAfterEnd(body,"<img src="+ClassLoader.getSystemResource("thumbnail.png").toString()+">");
            System.out.println(edPane.getText());
        } catch(BadLocationException e) {
        } catch (java.io.IOException e) {}


        frame.add(edPane);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);


    }

}

这篇关于使用pane.getDocument.insert()在JEditorPane中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆