Search code examples
javahtmlmultipart

Construct HTML Field?


I've been trying to go at this for a few hours now, and also searched the web, but simply can't find a solution.

I need to construct a Text Area (Whether it be JTextArea, JTextPane, or JEditorPane doesn't matter) which can read and format HTML.

I know that the JEditorPane can show HTML by giving it a Hyperlink, but what if I already got the HTML text, and just wants to show it..? If I use setText() it just shows a white field. Nothing in it.

The HTML text I get, is from an Email. I get it, using the following code (Just a snippet of it)

            String subject = message[row].getSubject();
            String from = InternetAddress.toString(message[row].getFrom());
            StringBuilder body = new StringBuilder();
            Multipart mp = (Multipart) message[row].getContent();
            for(int i = 0; i < mp.getCount(); i++) {
                BodyPart bp = mp.getBodyPart(i);
                String disp = bp.getDisposition();
                if(disp != null && (disp.equals(BodyPart.ATTACHMENT))) {
                    // Do something
                } else {
                    body.append(bp.getContent());
                }
            }
            EmailContent ec = new EmailContent(new JFrame(),true,from,subject,body.toString());
        } catch (IOException ex) {
            Logger.getLogger(MailPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(MailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Help?


Solution

  • import javax.swing.*;
    
    public class HTMLFromString {
    
        HTMLFromString() {
            JEditorPane jep = new JEditorPane();
    
            String html = "<html><body><h1>Title</h1><p>Paragraph..";
            // Important!
            jep.setContentType("text/html");
            jep.setText(html);
    
            JOptionPane.showMessageDialog(null, jep);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    new HTMLFromString();
                }
            });
        }
    }
    

    This version creates some HTML using (mostly) your snippet (with some actual content), prefixed with <html> to produce a result.

    import javax.swing.*;
    
    public class HTMLFromString {
    
        static String SNIPPET = 
                "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional" +
                "//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
                "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; " +
                "charset=Windows-1252\" /><title></title>" +
                "<style type=\"text/css\">" +
                "a, a:visited" +
                "{" +
                "  color: #406377;" +
                "}" +
                "</style>" + 
                "</head>" +
                "<body bgcolor=\"#ffffff\" style=\"background-color: #ffffff; " +
                "width: 600px; font-family: Arial, Verdana, Sans-serif; color: " +
                "#000; font-size: 14px; margin: 0px;\"><h1>Hi!</h1>";
    
        HTMLFromString() {
            JEditorPane jep = new JEditorPane();
    
            String html = "<html>" + SNIPPET;
            // Important!
            jep.setContentType("text/html");
            jep.setText(html);
    
            JOptionPane.showMessageDialog(null, jep);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    new HTMLFromString();
                }
            });
        }
    }