Results 1 to 7 of 7
  1. #1
    SirAiris is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Question 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.

    Java 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    SirAiris is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: displaying html in java (SWING)

    Displaying HTML in swing (JAVA) - Stack Overflow

    I posted it here, got no where.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: displaying html in java (SWING)

    Java Code:
    web.url
    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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: displaying html in java (SWING)

    You can simply do it with appending <html> tag at the start and end positions.

    [Blog spam removed]
    Last edited by DarrylBurke; 12-31-2012 at 04:25 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: displaying html in java (SWING)

    That is not the solution to the OPs problem in the slightest.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: displaying html in java (SWING)

    gowthamgutha, any more blog spam and you will be banned permanently. no further warning will be issued.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 07-26-2011, 11:50 AM
  2. How to embed java swing into a html
    By tomkonikkara in forum NetBeans
    Replies: 2
    Last Post: 07-11-2011, 08:10 AM
  3. Problem with displaying an image in Java Swing
    By BackInSoftware in forum Advanced Java
    Replies: 10
    Last Post: 01-11-2011, 06:56 PM
  4. Replies: 0
    Last Post: 07-19-2010, 10:25 PM
  5. Displaying a Java List in Html:select tag
    By ramitmehra123 in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 05:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •