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

HTMLEditorKit.write() makes zero-length output for small doc

    XMLWordPrintable

Details

    • kestrel
    • x86
    • windows_nt

    Description



      Name: wl91122 Date: 09/01/99


      I've had this problem on NT 4.0 (service pack 3) for JDK 1.2.1,
      1.2.2 and now 1.3beta.

      The simple program (attached) creates a JTextPane and two
      buttons - READ & WRITE - which call the
      JTextPane.getEditorKit().read() and write() methods to read or
      write data from/to a file called 'TEXT' to/from the document.
      The program is invoked with a single argument which is used as
      the parameter to the setContentType() method for the JTextPane.

      When run with arguments of 'text/plain' or 'text/rtf' one can
      enter a few words of text into the JTextPane and click on WRITE
      and the contents of the document will be successfully saved to
      the file 'TEXT'. Clicking on READ will read the saved text
      back into the JTextPane.

      But when one runs the program with a content type of 'text/html'
      the editorkit.write() call produces a zero-length output file.
      Certainly when one has entered just a few words/lines of text.

      Am I doing something wrong? I'd assumed that if one just typed
      in some text into the pane it would be saved as html, just as
      it is saved as rtf when a content type of 'text/rtf' is used.

      (If I can ask another question in passing - when using
      'text/html' for the content type, when I enter lines of text
      into the JTextPane they appear as "double-spaced"; how can
      I elminate the blank lines between entered lines of text?
      Thanks!)




      ------------------------------------------------------------

      /*
       * Synopsis:
       *
       * java TestTextBug content-type
       *
       * where content-type can be 'text/plain', 'text/html' or 'text/rtf', as per the
       * SDK API documentation for JTextPane.setContentType().
       *
       * The application sets up a simple frame with READ & WRITE buttons and a
       * JTextPane
       * with the supplied content type. If you use 'text/rtf' as the content type
       * then the WRITE button will write the current contents of the document to a fi
      le
       * named TEXT, and READ will read this file and load the document.
       *
       * However for a content type of 'text/html' WRITE produces a zero-length TEXT
       * file. Why is the editorKit.write() method doing this and not working?
       */

      import java.awt.*;
      import java.awt.event.*;
      import java.awt.image.*;
      import javax.swing.*;
      import javax.swing.text.*;
      import java.io.*;

      public class TestTextBug {
       static public void main (String args []) {
        if (args.length != 1) {
         System.err.println ("Usage: TestTextBug content-type-string");
         System.exit (2);
         }

        JFrame frame = new JFrame ("Test Text Bug");
        frame.addWindowListener (new WindowAdapter () {
         public void windowClosing (WindowEvent e) { System.exit (0); }
         });

        JPanel buttonPanel = new JPanel ();
        buttonPanel.setLayout (new GridLayout (0, 1));

        final JTextPane textpane = new JTextPane ();
        textpane.setContentType (args[0]);

        final EditorKit kit = textpane.getEditorKit ();

        final JScrollPane scroll = new JScrollPane (textpane);
        scroll.setPreferredSize (new Dimension (300, 150));

        /*
         * Set up the two buttons
         */
        JButton sb = new JButton ("Write document out to file TEXT");
        sb.addActionListener (new ActionListener () {
         public void actionPerformed (ActionEvent ev) {
          try {
           FileOutputStream out = new FileOutputStream ("TEXT");
           kit.write (out, textpane.getDocument(), 0, textpane.getDocument().getLength
      ());
           out.close ();
           System.out.println ("\n\nSupposedly wrote out " + textpane.getDocument().ge
      tLength() + " characters.\nNow writing document out to stdout:\n");
           kit.write (System.out, textpane.getDocument(), 0, textpane.getDocument().ge
      tLength());
           }
          catch (Exception ev2) { System.out.println ("Got an exception in write - " +
       ev2); }
          }
         });
        buttonPanel.add (sb);

        JButton rb = new JButton ("Read in from file TEXT");
        rb.addActionListener (new ActionListener () {
         public void actionPerformed (ActionEvent ev) {
          try {
           FileInputStream in = new FileInputStream ("TEXT");
           kit.read (in, textpane.getDocument(), 0);
           in.close ();
           }
          catch (Exception ev2) { System.out.println ("Got error - " + ev2); }
          }
         });
        buttonPanel.add (rb);

        Container contentPane = frame.getContentPane ();
        contentPane.setLayout (new BorderLayout ());
        contentPane.add (buttonPanel, BorderLayout.WEST);
        contentPane.add (scroll, BorderLayout.CENTER);

        frame.pack ();
        frame.setVisible (true);
        }
       }
      (Review ID: 94442)
      ======================================================================

      Attachments

        Activity

          People

            svioletsunw Scott Violet (Inactive)
            wleesunw William Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: