javaswingjeditorpane

Java Swing JEditorPane not outputting empty paragraphs


After plugging HTML into a JEditorPane, I'm unable to read the HTML back out if there's empty paragraph elements. This is important for my project because I'm using it as an html editor where user's can create new paragraphs, but if they don't enter any content into the paragraphs they get erased on the next editor.getText(), which is confusing for the user.

After the editor pane loads if I call .getText() on it without making any changes, it has the following HTML structure:

<html>
    <head>

    </head>
    <body>
        <p style="margin-top: 0">
  
        </p>
    </body>
</html>

But then if i simply call

editor.setText(editor.getText())
System.out.println(editor.getText())

The output changes to

<html>
    <head>

    </head>
    <body>
    </body>
</html>

I need to be able to get the HTML, modify it with JSoup, and re-insert it. Ideally i would have no loss at all when calling getText(). I've tried reading the HTML out instead using HTMLDocument:

        StringWriter writer = new StringWriter();    
        try {
            htmlKit_.write(writer,  htmlDoc_, 0, htmlDoc_.getLength());
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (BadLocationException e1) {
            e1.printStackTrace();
        }
        String s = writer.toString();
        System.out.println(s);

But it gives me the same results. Is there any way I can preserve these empty tags? I need to preserve empty list tags as well, empty table tags, etc. Here's my code. Thanks for your help

TESTEDITOR:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.io.*;
import javax.swing.text.*;

public class TestEditor implements ActionListener {

    JButton printHTMLButton_;
    JEditorPane editor_;
    HTMLEditorKit htmlKit_;
    HTMLDocument htmlDoc_;
    
    public TestEditor() {
        
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);     
        JPanel panel = new JPanel();
        frame.getContentPane().add(panel);
        panel.setBackground(Color.red);
    
        printHTMLButton_ = new JButton("HTML");
        printHTMLButton_.addActionListener(this);
        panel.add(printHTMLButton_);
        
        editor_ = new JEditorPane("text/html", "");
        editor_.setEditable(true);
        editor_.setPreferredSize(new Dimension(500, 500));
        panel.add(editor_);
        
        htmlKit_ = new HTMLEditorKit();
        editor_.setEditorKit(htmlKit_);
        
        htmlDoc_ = (HTMLDocument)editor_.getDocument();
        
        frame.setVisible(true); 
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == printHTMLButton_) {
            System.out.println(editor_.getText());
            editor_.setText(editor_.getText());
            System.out.println(editor_.getText());


            StringWriter writer = new StringWriter();    
            try {
                htmlKit_.write(writer,  htmlDoc_, 0, htmlDoc_.getLength());
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (BadLocationException e1) {
                e1.printStackTrace();
            }
            String s = writer.toString();
            System.out.println(s);
        }
    }

    public static void main(String[] args) {
        new TestEditor();
    }
}

Solution

  • htmlKit_.write(writer,  htmlDoc_, 0, htmlDoc_.getLength());
    

    Last night, for your previous question, I was playing with the above statement to try to get only the HTML for your paragraph (so the bold tag would also be included). However, no matter what parameters I passed for the start/length I always got the entire document.

    Today, I played with the length:

    //htmlKit_.write(writer,  htmlDoc_, 0, htmlDoc_.getLength());
    System.out.println(htmlDoc_.getLength());
    htmlKit_.write(writer,  htmlDoc_, 0, 2);
    

    and magically the paragraph tag is now included in the output.

    The write(...) method of the editor kit obviously has some problem.