Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6699858

HTMLEditorKit.ParserCallback does not automatically flush at end of html read

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.6.0_03"
      Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
      Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 2.6.22-14-generic #1 SMP Tue Feb 12 07:42:25 UTC 2008 i686 GNU/Linux
      Ubuntu Hardy release

      A DESCRIPTION OF THE PROBLEM :
      When Loading html into a JEditorPane,
      if the HTML does not contain enough content to cause a scrollbar to appear (and, i guess, cause a repaint?) flush() is never called on the HTMLEditorKit.ParserCallback, and the content is not made visible on the JEditorPane.
      Subsequent resizing of the frame does NOT cause the html text to appear.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. run the sample java code given below using the given html (below)
       
      <!DOCTYPE html
      PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html>
      <head>
      <title>simple document</title>
      </head>
      <body>
      <p>a simple paragraph</p>
      <p>another simple paragraph</p>
      </body>
      </html>





      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected to see the text appear in the EditorPane
      ACTUAL -
      1. No text appeared in EditorPane. Subsequent resizing of the frame does NOT cause the html text to appear.


      2. increase the number of lines in the html so as to cause a scrollbar to kick in, run sample code again.
           (in this run, text will appear in the EditorPane)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.astraia;

      /*
       Definitive Guide to Swing for Java 2, Second Edition
       By John Zukowski
       ISBN: 1-893115-78-X
       Publisher: APress
       */

      import java.awt.BorderLayout;
      import java.awt.Container;
      import java.awt.event.ActionEvent;
      import java.io.FileReader;
      import java.io.IOException;

      import javax.swing.AbstractAction;
      import javax.swing.Action;
      import javax.swing.JButton;
      import javax.swing.JEditorPane;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.text.JTextComponent;
      import javax.swing.text.html.HTMLDocument;
      import javax.swing.text.html.HTMLEditorKit;
      import javax.swing.text.html.parser.ParserDelegator;

      public class HtmlViewer {

      public static void main(String args[]) {
      final String filename = "/home/sean/Desktop/tinytest.xhtml";
      JFrame frame = new JFrame("Loading/Saving Example");
      Container content = frame.getContentPane();

      final JEditorPane editorPane = new JEditorPane();
      // editorPane.setEditable(false); // uncomment this to make comment invisible
      JScrollPane scrollPane = new JScrollPane(editorPane);
      content.add(scrollPane, BorderLayout.CENTER);

      editorPane.setEditorKit(new HTMLEditorKit());
      editorPane.setBackground(new java.awt.Color(252, 171, 193));
      editorPane.setContentType("text/html");

      JPanel panel = new JPanel();

      // Setup actions
      Action loadAction = new AbstractAction() {
      {
      putValue(Action.NAME, "Load");
      }

      public void actionPerformed(ActionEvent e) {
      doLoadCommand(editorPane, filename);
      }
      };
      JButton loadButton = new JButton(loadAction);
      panel.add(loadButton);

      content.add(panel, BorderLayout.SOUTH);

      doLoadCommand(editorPane, filename);

      frame.setSize(550, 550);
      frame.setVisible(true);
      }

      public static void doLoadCommand(JTextComponent textComponent, String filename)
      {
      FileReader reader = null;
      try
      {
      System.out.println("Loading");
      reader = new FileReader(filename);

      // Create empty HTMLDocument to read into
      HTMLEditorKit htmlKit = new HTMLEditorKit();
      HTMLDocument htmlDoc = (HTMLDocument) htmlKit
      .createDefaultDocument();
      // Create parser (javax.swing.text.html.parser.ParserDelegator)
      HTMLEditorKit.Parser parser = new ParserDelegator();
      // Get parser callback from document
      HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
      // Load it
      parser.parse(reader, callback, false);
      // callback.flush(); // this is a work-around. Uncomment to use
      // Replace document
      textComponent.setDocument(htmlDoc);
      System.out.println("Loaded");

      } catch (Exception exception) {
      System.out.println("Load oops");
      exception.printStackTrace();
      } finally {
      if (reader != null) {
      try {
      reader.close();
      } catch (IOException ignoredException) {
      System.out.println("exception " + ignoredException);
      }
      }
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use callback.flush() after parsing the document.

      eg.

      parser.parse(reader, callback, false);
      callback.flush();

            peterz Peter Zhelezniakov
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: