Skip to content Skip to sidebar Skip to footer

Can A User-chosen Image Be Inserted Directly Into A JEditorPane?

What I am trying to do is open up a JFilechooser that filters jpeg,gif and png images, then gets the user's selection and inserts it into the JEditorPane. Can this be done? or am i

Solution 1:

Its easier to just use a JTextPane. Then you can use insertIcon(...) anywhere in the text.

Edit:

I have never had much luck trying to manipulate HTML but I've used code like the following before:

HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, textPane.getCaretPosition(), text, 0, 0, HTML.Tag.A);

So presumably the code would be similiar for the IMG tag.


Solution 2:

This should do it:

mainText.setContentType("text/html");
String image = String.format("<img src=\"%s\">", imageChooser.getSelectedFile());
mainText.setText(image);

Post a Comment for "Can A User-chosen Image Be Inserted Directly Into A JEditorPane?"