JEditorPane - HTML Problem

Hello people:
I am new to your forum and have this problem: I am using a JEditorPane component in my GUI and wish to fornat it using text/html. This I have done. I also want to dynamically add content to it from functions in my program, but this is not possible as, when the first content is added to the Pane, it is terminated with a </html> tag. Is there a way that I can specify it not to do this so that I can dynamically add my HTML content throught the life of my program.
Regards and thanks in advance,
mintsmike

camickr wrote:
Cross postedThanks for the heads-up.
db

Similar Messages

  • Puting into JEditorPane HTML file

    I want to put into JEditorPane HTML file from my local drive.
    How to do that?

    Hear is example try it.
    *(c)pesilEX - 2007
    * [email protected]
    * Let�s make a open source software world
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.URL;
    public class pesilEX extends JFrame implements ActionListener{
    JPanel cp;
    // Declaring a url to get file name
    // Also this can use to get url form online
    public URL helpURL;
    JScrollPane scrol;
    JEditorPane htmlPane;
    JButton btn1;
    // This string is use to store file name
    String fileName;
    public pesilEX(){
         cp = (JPanel)getContentPane();
         cp.setLayout(null);
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    scrol = new JScrollPane();
    scrol.getViewport().add(htmlPane);
    scrol.setBounds(10,10,370,300);
    cp.add(scrol);
    btn1 = new JButton("View!");
    btn1.setBounds(260,320,120,20);
    cp.add(btn1);
    btn1.addActionListener(this);
    public void actionPerformed(ActionEvent e){
         if(e.getSource()==btn1){
         // File name you can replace is as you want
    fileName = "pesilEX/help.htm";
    // Import html file to java
         helpURL = getClass().getClassLoader().getResource(fileName);
    try{
    // Set url to JEditorPane
         htmlPane.setPage(helpURL);
    catch(Exception er){
         System.out.println(er.toString());
    public static void main(String[] args){
    pesilEX pesil = new pesilEX();
              pesil.setSize(400,400);
              pesil.setTitle("pesilEX JHtml viewer");
              pesil.setResizable(false);
              pesil.setVisible(true);
    }

  • Send html problems

    Hello,
    I need help with my problem, this is part of my procedure that will send mail
    c := utl_smtp.open_connection(server);
    utl_smtp.helo(c, domena);
    utl_smtp.mail(c, posilja);
    utl_smtp.rcpt(c, prejemnik);
    utl_smtp.open_data(c);
    send_header('From', '<' || posilja || '>');
    send_header('To', '<' || prejemnik || '>');
    IF posljicc is not null THEN
         send_header('Cc', '<' || posljicc || '>');
         poslji_mail(posljicc, null, zadeva, posta);
    END IF;
    send_header('Subject', zadeva);
    send_header('MIME-Version', '1.0' || utl_tcp.CRLF);
    send_header('Content-Type', 'text/html' || utl_tcp.CRLF); <----- PROBLEM
    utl_smtp.write_data(c, utl_tcp.CRLF || posta);
    utl_smtp.close_data(c);
    utl_smtp.quit(c);...
    and it works fine but when I want to send HTML mail I see mail in code not as html like this:
    Content-Type: text/html <------- this should be in header
    <HTML><HEAD><META http-equiv="Content-Type" content="text/html; charset=windows-1250"></HEAD><BODY><table><tr><td>1</td><td>2</td></tr><tr><td>1</td></tr><tr><td>1</td><td>2</td><td>3</td></tr></table></body></html>
    Problem is that I see mail as text and I also see this in text body "Content-Type: text/html", but this should be in header.
    Can anyone help me with my problem, what must I do to get the html mail (not as text)?
    Thanks for your help

    Thanks I have solved the problem

  • JEditorPane html display problem

    Hello,
    I'm running a JEditorPane inside an Applet for a project im working on. The source included below is an over-simplified version that produces the same results. The problem i'm having is that the html displays in an odd fasion in the JEditorPane.
    Since I cant include a screenshot let me attempt to describe it. Regardless of the FONT face in the html the paragraph displays as you would expect in the JEditorPane until the last line. The last has extra horizontal space separating it from the rest of the paragraph. This seems completely arbitrary. There is nothing in the html code near the line break.
    I've linked this problem to the font face attribute tags. Regardless of the font I choose it displays improperly. Any idea on how to remove this spacing problem in my JEditorPane?
    I'm using NetBeans 3.5.1 and SDK 1.4.2. I've tested this problem in the AppletViewer as well as online in a web browser.
    * CourierTest.java
    * Created on July 24, 2004, 8:28 PM
    import java.awt.Font;
    import java.io.*;
    * @author LD Miller
    public class CourierTest extends javax.swing.JApplet {
    String html;
    /** Initializes the applet CourierTest */
    public void init() {
    initComponents();
    html = "<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT size=3><FONT face=\"Times New Roman\">What is event-driven programming?It is a <I>computer programming</I> paradigm that is different from traditional, sequential programming. In traditional, sequential programming, one could follow the program and plot out exactly the sequence of execution of the program. However, in event-driven programming, it is not possible to plot out exactly the sequence of execution of the program since flow of the execution is determined by the events. Event-driven programming allows a program to interact with users and re-act to user-generated events in a more efficient and effective manner. </FONT></FONT></P>";
    jTextPane1.setContentType("text/html");
    jTextPane1.setText(html);
    /** This method is called from within the init() method to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    private void initComponents() {
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextPane1 = new javax.swing.JTextPane();
    jScrollPane1.setViewportView(jTextPane1);
    getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration

    1) I have complex html page (having lot of html controls).
    .when i resize the container, controls gets overlapped.
    shall we avoid this? to my experience JEditorPane deviates from the display of typical browsers when HTML is displayed that contains complex table layouts (tables nested in other tables or non-explicit or non-existent size expressions in tables for instance). I recommend to try and simplify layout complexity as a workaround. Start with a most simple page and increase complexitiy until display gets unsatisfactory.
    2) I used JEditorPane. My html contents are stored in
    text. i called setText()...whether i need to use
    HTMLDocument, HTMLEditorKit etc...to render
    html in better way...I did not understand your second point, what is the question?
    Ulrich

  • JEditorPane HTML font style changing problems

    I have tried changing the font size using the action:
    StyledEditorKit.FontSizeAction("12", 12)
    but that seems to have problems. When I change it on the editor and write and re-read the change html the font size does not look the same.
    Is there a bug in changing the font size of htmls using the JEditorPane or JTextPane component?
    How/What does the "font-change-bigger" or "font-change-smaller" work with in the HTMLEditorKit? Should I be using these members somehow instead?

    Hello,
    When you use HTMLEditorKit, you can not specify font out size of HTML. For Example, use FONT tag in html to set your font. There are many ways to change font in html. tags like h1, h2 etc..
    O.K.

  • JEditorPane, CSS, HTML problem

    Hi,
    I am using setEditorKit(new HTMLEditorKit()) with a JEditorPane to render / display a HTML file. The HTML has a stylesheet with it.
    The HTML file has a table in it. When I try to use the CSS values of
    TABLE.black-border { border-width:1px;
                         border-color:black;
                         border-collapse: collapse;
    tr.bb-test { border-width:1px;
                 border-color:black;
                 border-style:solid;
                 border-collapse: collapse;
    td.bb-medium { border-collapse: collapse;
                   border-width:1px;
                   border-color:black;
    <TABLE class=black-border WIDTH=100%>
    <TR>
    <TD class=bb-medium WIDTH=10%>TEST</TD>
    <TD class=bb-medium WIDTH=25%>TEST Number</TD>
    <TD class=bb-medium WIDTH=55%>POLICY Range</TD>
    <TD class=bb-medium WIDTH=10%>DEST DATE</TD>
    </TR>
    <TR class=bb-test>
    <TD class=bb-medium COLSPAN=4>item 1</TD>
    </TR>
    <TR class=bb-test>
    <TD class=bb-medium COLSPAN=4>item 2</TD>
    </TR>
    </TABLE>No matter what I set the border-width to (1, 10, 100), it always comes out as a thick border (same size). But in a web browser, 1, 10 or 100px border looks correct.
    When I try to set the individual borders (i.e. border-top-width ), the JEditorPane shows no border at all (as if it is invalid keyword).
    Does anybody know what the problem is? Any help would be appreciated.
    Thanks
    Roger...

    Better use JTextPane ,where it supports CSS to some extends.
    (I have used in my case for rendering font size and font face).Try to
    use external stylesheet instead of internal ones. Here I paste some part of the code.
              kit = new HTMLEditorKit();//kit s ur HTMLEditorKit
              doc = (HTMLDocument) (kit.createDefaultDocument());
              this.setEditorKit(kit);//'this' is ur JTextPane object
              this.setDocument(doc);
              this.setContentType("text/html");
              css = kit.getStyleSheet();//css s the StyleSheet object
              css.addRule("body {align : left }");//replace this part by ur CSS attributes for Table rendering
              css.addRule("body {font-size : 14pt; }");
              css.addRule("body {font-family : Sans Serif; }");
              css.addRule("div {margin-top: -20pt; }");
              css.addRule("div {margin-bottom: -20pt; }");Let me know weather this works out.

  • JEditorPane with html problem

    HI
    I have a JEditorPane with html, but this looks center, I mean that the text and images of the html look center in the JEditorPane. What can I do?

    No, I don't want center text in the JEditorPane, in this moment I did a html with justify alignment, on the browser the html looks ok, but in the JEditorPane looks center, the text and images looks center in the JEditorPane, How do I do to that the html looks as on the browser.
    Thanks for your help

  • JEditorPane/HTML image loading delay

    I am using a JEditorPane to render HTML to a graphics buffer which I then write as an image file. Since HTML loading is asynchronous I wait for the "page" property change event after calling setPage() before using print() to capture the rendered HTML to a graphics buffer. Unfortunately, the "page" property change event is fired after the HTML is loaded but before images referenced by the HTML are rendered. As a result, the image that I capture has little picture icons in place of the graphics. If I sleep for a short time before capturing the image, it works fine. However, sleeping is not an acceptable solution as there is no way to predict how long I will need to sleep because it varies based on system load and complexity of the HTML being rendered. Is there any way to check if an HTML document has completely loaded including all the images? Does anyone have a better way to capture an image of a rendered HTML document that would not have this problem?
    Below is a code snippet that shows what I am trying to do.
    static final JEditorPane p;
    static final Object done;
    static {
        done=new Object();
        p=new JEditorPane();
        p.setSize(1728, 2156);
        p.addPropertyChangeListener("page", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) {
            synchronized(done) {
                done.notifyAll();
    static void capture(File htmlFile) {
        BufferedImage img=new BufferedImage(1728, 2156, BufferedImage.TYPE_BYTE_GRAY);
        synchronized(done) {
            p.setPage(htmlFile.toURL());
            try {
                done.wait();
            } catch(InterruptedException e) {}
    /* without this code images are not rendered
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {}
        Graphics g=img.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 1728, 2156);
        p.print(g);

    HI Sarnoth,
    I'm trying to do something similar and have used your code, but it doesn't work completely for me. Do you see anything that I'm doing wrong? I am trying to save the HTML page as a JPEG image. This is my code and I'm having problems. Anyone have any ideas? It is dying on the je.encode(bi) line. Also, for some reason, if I uncomment the commented out lines, it hangs. Any ideas on that one too?
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import javax.swing.JEditorPane;
    import com.sun.media.jai.codec.ImageCodec;
    import com.sun.media.jai.codec.ImageEncoder;
    import com.sun.media.jai.codec.JPEGEncodeParam;
    public class HTMLPageToImageConverter
         private HTMLPageToImageConverter() {
         static final JEditorPane p;
         static final Object done;
         static {
             done = new Object();
             p = new JEditorPane();
             p.setSize(1728, 2152);
    /*         p.addPropertyChangeListener("page", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) {
                 synchronized(done) {
                     done.notifyAll();
         static void GenerateHTMLBufferedImage(File htmlFile, BufferedImage img) {
             img = new BufferedImage(1728, 2152, BufferedImage.TYPE_BYTE_GRAY);
             synchronized(done) {
                 try {
                      p.setPage(htmlFile.toURL());
                 } catch(IOException e1) {}
    /*             try {
                     done.wait();
                 } catch(InterruptedException e) { System.out.println("GenerateHTMLBufferedImage InterruptedException"); }
             Graphics g = img.createGraphics();
             g.setColor(Color.white);
             g.fillRect(0, 0, 1728, 2152);
             p.print(g);
         public static boolean SaveHTMLFileToJPEG(String htmlFile, String jpegFile) {
              try {
                   BufferedImage bi = null;
                   GenerateHTMLBufferedImage(new File(htmlFile), bi);
                   FileOutputStream fos = new FileOutputStream(jpegFile);
                   JPEGEncodeParam jp = new JPEGEncodeParam();
                   ImageEncoder je = ImageCodec.createImageEncoder("JPEG", fos, jp);
                   je.encode(bi);
                   fos.close();
              } catch(FileNotFoundException fnfe) { System.out.println("SaveHTMLFileToJPEG FileNotFoundException"); }
                catch(IOException ioe) { System.out.println("SaveHTMLFileToJPEG IOException"); }
              return true;
    }Called from other code
    HTMLPageToImageConverter.SaveHTMLFileToJPEG("d:/page1.html", "d:/page1.jpeg");
    ...Thanks in advence,
    --Ed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • JEditorPane html

    I have a simple JEditorPane set up so that I can view source code and rendered version of an html page. I toggle between rendered and source view by setting the content type:
    setContentType("text/plain") - displays source code
    setContentType("text/html") - dispalys the rendered view
    The problem I have is with offsets. It would be natural to me that the first letter in the rendered view starts at 0 and ends at 1. That's not the case however. In any test page I have, the first letter has offset 3-4. The same things happen inside the text as well. When I count offsets by hand, they are different that what getSelectionStart() and getSelectionEnd() show.
    I'm assuming that there must be some characters that get rendered by JEditorPane but don't get displayed - but quite frankly I have no clue as to why it's happening.
    Any ideas would be appreciated. I wrote a simple algorithm (irrelevant to this post) that uses the rendred view offsets to mark things in the source view, but since my rendered view offsets are screwed up - I'm not able to mark things properly.
    Thanks
    M

    Have you tried displaying the characters between offsets 0-2 ? It could give you an idea of what it concerns...

  • JEditorPane HTML preview

    Hi,
    I've been working on a small class which will render out HTML. There's a few examples on the web, all of which read an HTML file from a URL. JEditorPane works fine for that use, but when I try to specify HTML content as a string the page doesn't get displayed correctly. It's a bit hard to describe but basically if you specify a document as type text/html it tries to generate it's own <html>, <body> tags etc and leaves yours out. If you do happen to specify your own then it just ignores your page content.
    I need to do it that way as the HTML doesn't exist at a URL, only in memory. Does anyone know of a workaround or has anyone else experienced this problem? I'm developing the application using JDK 1.4.0 under Win2k.
    Thanks

    Yeah that's pretty much exactly what I'm doing, although it's pulling the HTML content from the database first.
    Your code outputs this:
    <html>
    <head>
    </head>
    <body>
    <b>Bolloxs</b>
    </body>
    </html>
    As you can see, it's being altered by the application and has had the <head> tags added. In my application it's doing even more, stripping everything inside of the <body> tags.
    I've found the reason though...it doesn't seem to like a meta tag.
    This content:
    String html = new String("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><html><head><title>This is my title</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head><body leftmargin=\"0\" topmargin=\"0\"><table width=\"600\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>Page content goes here</td></tr></table></body></html>");
    Outputs:
    <html>
    <head>
    </head>
    <body>
    <p>
    </p>
    </body>
    </html>
    Whilst this content (with the meta line removed):
    String html = new String("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><html><head><title>This is my title</title></head><body leftmargin=\"0\" topmargin=\"0\"><table width=\"600\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>Page content goes here</td></tr></table></body></html>");
    Outputs:
    <html>
    <head>
    <title>This is my title </title>
    </head>
    <body leftmargin="0" topmargin="0">
    <table border="0" cellpadding="0" width="600" cellspacing="0">
    <tr>
    <td>
    Page content goes here
    </td>
    </tr>
    </table>
    </body>
    </html>
    Weirdness!?

  • Preferred size of JEditorPane/ HTML page

    Hi,
    I've loaded an HTML page in a JEditorPane. Since I'm using a layout manager with absolute positioning, I need to know the pane's preferred size.But the preferred size I get is just about (6, 6), although it has a big picture in it. I tried validate() first or to add it with (1,1) to have it set up properly (visible etc.) and then resize it afterwards, but it had no effect. Is there a way to get a preferred size of an HTML page?
    Thanks a lot.
    Greets
    Puce
    Note: Using another layout manager is not a option, since this is a very specialized task.

    I could use this as well. I'm tiling JEditorPanes vertically inside a JPanel using BoxLayout. The problem is that I need to limit the width of the panel, so I need to adjust the sizes accordingly. Any suggestions? I'll give another duke...
    Max

  • Named Anchor in JEditorPane HTML

    I am loading an HTML string into a JEditorPane.
    The string has some named anchors (<A NAME="blah">) that reference other locations in the HTML string.
    Because I am loading the HTML string by using JEditorPane.setText, the named anchors do not work. This seems to be a problem when my HyperLinkListener tries to find the target URL.
    Does anyone know how to solve this problem?

    Did you find a solution for this problem in the meantime? I'd like to implement an online help feature in my newest program. I wrote a HTML text with anchors/links and the program loads this text to an JEditorPAne witch HTMLEditorKit by using the EditorPanes 'read' method. But all I got are HyperLinkExceptions from the HyperlinEventListener.
    regards, hgw2

  • JEditorPane +html generation

    Hi,
    I am using JEditorPane as WYSIWIG editor for html, everythings fine, but when i get html source (with getText()) i have font attributes ok (bold, italic etc.), but paragraph attributes are not inserted, i cannot handle with this problem and my JDK documentation:), if you know where should I look or something else I will by very gratefull. Thanks

    Hi
    I'm setting tekst with methods:
    StyleConstants.setAlignment(oAttributes, StyleConstants.ALIGN_RIGHT);
    oDocument.setParagraphAttributes(startPos, endPos-startPos, oAttributes, false);
    and I see in JEditorPane everythings ok (paragraphs are in place where they should be)
    but getText
    returns full html source (with <html><head></head><body>...</body></html>) and with <b>some bold</b><i>some underline</i>
    but without any tag that can be used to set paragraph and alignment of this paragraph (without any tag like <p> etc.), so this html is not correct to the view of the editor.
    Thanks for help

  • JEditorPane, html, tables and images

    I am trying to use jEditorPane to display an HTML document. If images are included into cells of a table, the width and height of the cells do not adjust to the size of the images (as it happens with common web browsers), no matter what parameters I use for TD and IMG tags.
    Do you experience the same problem? Is there a solution?
    Thanks.
    Ruben Razquin

    Hi Ruben,
    you will have to implement adjustment of table cells yourself to achieve this. With the existing way JEditorPane et. al. render tables table cells always are sized either in default sizes or by attributes explicitly giving the size (such as attribute WIDTH).
    Ulrich

  • JEditorPane Html Table Lines

    It seems if I load html containing tables into a JEditorPane, the format is less than great. The main problem I have is that none of the table lines show up. I was wondering if there was an option or something I'm overlooking, that would solve this problem. JEditorPane.showTheLinesSeperatingRowsAndColumnsInHtmlTables(true) for instance would be great.
    Thanks

    if the HTML source produces line, JEditorPane should shouw them as long as HTML 3.2 is used.
    Ulrich

Maybe you are looking for

  • Creating New Process Types

    Hello, Great to hear from anyone who has created a non-ABAP process type and included in the process chain. The interface methods and how they get called and/or check status etc. are not documented or at least I cannot find them. Thanks Mathew

  • Mail will not open hot links

    Mail has suddenly stopped opening hot links when lonk is clicked i get a mail error message and "no associated application could be found" I have not changed any settings

  • In my computer doesnt apear the pictures

    I have a problem in my computer donnt apear the pictures

  • How to create an class object

    Why we need set the 'Create object xx' between 'class definition' and  'class implementation'?? If i set the 'CREATE OBJECT' under the 'Class implementation' in a report,  the system will tell me that: 'Statement is not accessible'. This is my code:

  • External Hard Disc - Partitions - Using TM

    I have upgraded my iMac to Leopard and installed an Iomega minimac 500 Gb EHD. I searched for advice on discussions and then created 3 partitions. 60 Gb Rescue, for clone of Leopard, 80Gb Last, for previous Panther clone, 325Gb for Back up. Query is: