jEditorPane处理本地图像

时间:2014-09-15 01:28:41

标签: java image jeditorpane

我正在使用jEditorPane在wysiwyg编辑器中工作,现在我正试图通过JFileChooser将图像插入de editorpane,但它只插入一个图像,我不知道该怎么做,任何想法?

以下是我插入图片的方式:

public void Imagen(){

int im = ImageChooser.showOpenDialog(ImageChooser);
    if (im == JFileChooser.APPROVE_OPTION){


            String Path = String.format("<img src=\"file:\\%s\"alt=\"Image\">", ImageChooser.getSelectedFile().getAbsolutePath());
            EditorPane.setText(Path );


      System.out.println(Path);

        }
}

1 个答案:

答案 0 :(得分:0)

嗯......不知怎的,我设法解决了我的问题,似乎setText并不擅长。我所做的是用.getAbsolutePath获取路径(我打印它只是为了确保我以正确的方式做到这一点)然后用HTMLEditorKit插入它,你必须小心这里&# 39;因为我最初看不到图像,因为我写错了,无论如何我的解决方案:

public void Imagen(){

int im = ImageChooser.showOpenDialog(ImageChooser);
    if (im == JFileChooser.APPROVE_OPTION){
        System.out.println("Loading Image...");
        int caretPos = EditorPane.getCaretPosition();
        HTMLEditorKit e = new HTMLEditorKit();
     try {


    String Path = String.format( ImageChooser.getSelectedFile().getAbsolutePath());
               System.out.println(Path);
         e.insertHTML(document, caretPos,"<img src=\"file:\\"+Path+"\" alt=\"some_text\">" , 0, 0, HTML.Tag.IMG);
       //  <img src="url" alt="some_text">

     } catch (BadLocationException ex) {
         Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
     } catch (IOException ex) {
         Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
     }


        }else{
            System.out.println("Nothing loaded");}
}