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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.