displaying html in java (SWING)
I am working on a project of sorts to load HTML files from a server and display them in swing.
Code:
import java.io.*;
import java.net.*;
import java.util.regex.*;
import javax.swing.*;
public class webloader {
public static void loadcode(){
URL url = null;
try {
url = new URL("web"+File.separator+web.url+File.separator+"index.html");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection con = null;
try {
con = url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
Matcher m = p.matcher(con.getContentType());
String charset = m.matches() ? m.group(1) : "ISO-8859-1";
Reader r = null;
try {
r = new InputStreamReader(con.getInputStream(), charset);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
StringBuilder buf = new StringBuilder();
while (true) {
int ch = 0;
try {
ch = r.read();
} catch (IOException e) {
e.printStackTrace();
}
if (ch < 0)
break;
buf.append((char) ch);
}
String str = buf.toString();
JFrame mainframe = new JFrame(web.url);
mainframe.setSize(800, 750);
mainframe.setResizable(false);
JPanel website = new JPanel();
JLabel webcontent = new JLabel(str);
website.add(webcontent);
mainframe.add(website);
mainframe.setVisible(true);
}
}
Error:
#5498427 - Pastie
PS. I am quite new to java, so if I seem to be stupid or not know what I am doing, that's because I am.
Re: displaying html in java (SWING)
I've seen this question elsewhere -- have you cross-posted this question on a different site? If so, please notify us with links to the other site.
Re: displaying html in java (SWING)
Re: displaying html in java (SWING)
on line 11.
You have no 'web' variable.
This code does not compile, so trying to run it is a bit pointless (which is sort of what that error is telling you).
Also, I would recommend actually posting the stack trace and error along with your post.
Many people won't bother going to links.
Re: displaying html in java (SWING)
You can simply do it with appending <html> tag at the start and end positions.
[Blog spam removed]
Re: displaying html in java (SWING)
That is not the solution to the OPs problem in the slightest.
Re: displaying html in java (SWING)
gowthamgutha, any more blog spam and you will be banned permanently. no further warning will be issued.
db