jEditorPane处理本地图像 [英] jEditorPane handling local images
问题描述
我正在使用jEditorPane在所见即所得的编辑器中工作,现在我正尝试通过JFileChooser将图像插入到de editorpane中,但是它只能插入一个图像,我不知道该怎么做,任何想法?
I'm working in a wysiwyg editor, with a jEditorPane, right now I'm trying to insert images via a JFileChooser into de editorpane, but it only inserts one image, I don't know how to do it, any ideas?
这是我插入图像的方式:
Here's how I'm inserting the image:
公共无效Imagen(){
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);
}
}
推荐答案
好吧……以某种方式设法解决了我的问题,看来setText
一点也不好.相反,我要做的是获取.getAbsolutePath
的路径(我打印出来只是为了确保我以正确的方式进行打印),然后将其插入HTMLEditorKit
,您在这里要小心,因为我不是之所以能够首先看到该图像,是因为我写错了,无论如何,这里都是我的解决方案:
Well... somehow I managed to solve my problem, it seems setText
wasn't good at all. What I did instead was get the path with .getAbsolutePath
(I printed it just to be sure I was doing it the right way) and then insert it with the HTMLEditorKit
, you got to be careful here 'cause I wasn't able to see the image at first because I was writing it incorrectly, any way here my solution:
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");}
}
这篇关于jEditorPane处理本地图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!