954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

JTextPane

Hi everyone,

I am trying to insert a image into the jtextpane using the html document as the default document for the jtextpane. It seems that nothing happens when i try to insert the image from disk.

htmldoc - an instance of the HTMLDocument class
TextPane1 - an instance of the JTextPane class
mas - an instance of the MutableAttributeSet class

Here is my code

The file chooser class is ok so i did not include it here

File g = FileChooser3.getSelectedFile();
ImageIcon image1 = new ImageIcon(g.toString());

int w = image1.getIconWidth();
int h = image1.getIconHeight();

mas.addAttribute(StyleConstants.NameAttribute, HTML.Tag.IMG);
mas.addAttribute(HTML.Attribute.SRC, g.toString());
mas.addAttribute(HTML.Attribute.HEIGHT, Integer.toString(h));
mas.addAttribute(HTML.Attribute.WIDTH, Integer.toString(w));

try
{
int p = TextPane1.getCaretPosition();
htmldoc.insertString(p, " ", mas);
}

catch(Exception e)
{
}

The above code compiles without errors but does not seem to work.
Can someone tell me how to insert an image into a html document in the jtextpane or show me an example of inserting icons in a html document

Thank You

Yours Sincerely

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

in src attribute you have to add "file:" at the begining like this

mas.addAttribute(HTML.Attribute.SRC, "file:"+ pictureFileName);

pictureFileName contains an absolute path to file, but it should work with relative aswell
also, i'v changed mas to SimpleAttributeSet
and the code works fine

ps. Your problem helped to solve me mine, so thank you

arseniew
Newbie Poster
14 posts since Jan 2009
Reputation Points: 14
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You