Google image shown as broken in HTMLEditorKit

D

daly.ronan

Hello,

I am trying to get the google logo to display in a htmleditorkit and I
can't get it to work. When I try to display other images in the
htmleditorkit it works fine. I pasted in some code to demonstrate what
I am talking about. I would greatly appreciate it if someone could help
me figure this out. I am pretty sure the google server is preventing me
from grabbing the image. I say this because when I first went to read
one of their RSS feeds I got an error "Server returned HTTP response
code: 403". Once I set the urlconnection User-agent property to
"Mozilla/4.0" everything worked fine.

Thanks for the help.


import java.awt.Color;
import java.io.StringReader;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class DisplayImage extends JFrame
{
static DisplayImage test = null;
JPanel panel;
JEditorPane JEP;
HTMLEditorKit kit;
HTMLDocument doc;
String htmlStr = "<html><table><tr><img
src='http://news.google.com/intl/en_us/images/news_res.gif'></img></tr><tr><img
src='http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif'></img></tr><table></html>";

public DisplayImage()
{
buildUI();
}
public void buildUI()
{
panel = new JPanel();
panel.setBackground(Color.white);

JEP = new JEditorPane();
kit = new HTMLEditorKit();
JEP.setEditorKit(kit);
doc = (HTMLDocument)JEP.getDocument();

try { kit.read(new StringReader(htmlStr), doc, 0); }
catch (Exception e) { e.printStackTrace(); }

panel.add("Center", JEP);
getContentPane().add(panel);

setSize(200,200);
setVisible(true);
}
public static void main(String args[])
{
test = new DisplayImage();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top