This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

missing the content when saving HTML from JEditorPane.write(..)  RSS feed

 
michael byrd
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i m trying to develop html editor using JEditorPane. the document type used is HTMLDocument and HTMLEditorKit. when I try to save using the following function:


try{
FileWriterw = new FileWriter("doc.html");

//HTMLEditorKit edi = (HTMLEditorKit)editorPane.getEditorKit();
EditorKit edi = editorPane.getEditorKit();
//StyledEditorKit edi = new StyledEditorKit();
//edi.write(w, (StyledDocument)editorPane.getDocument(), 0, editorPane.getDocument().getLength());
editorPane.write(w);
w.close();
}catch(Exception de){System.out.println(de);
}



all the contents are missing, only the tags are intact in the output file doc.html. I am using JDK 1.4.2

anybody can help me? thank you very much


Re: missing the content when saving HTML from JEditorPane.write(..)
Author: hiwa Posts: 1,657 Registered: 99/03/29
Jan 20, 2006 3:55 AM (reply 1 of 4)


You should do a synchronous load of the page. Otherwise setPage() returns immediately before
page is fully loaded. Hence, your doc.html is written as a blank template.

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.*;
import java.io.*;

public class CaesarEp{

public static void main(String[] args) throws Exception{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = frame.getContentPane();

JEditorPane editorPane = new JEditorPane();
JEditorPane.registerEditorKitForContentType
("text/html", "HTMLEditorKit2");
editorPane.setEditorKitForContentType
("text/html", new HTMLEditorKit2());
editorPane.setPage("http://java.sun.com/");

con.add(editorPane);
frame.setSize(600, 600);
frame.setVisible(true);

FileWriter w = new FileWriter("doc.html");
editorPane.write(w);
w.close();
}
}

class HTMLEditorKit2 extends HTMLEditorKit{
public Document createDefaultDocument(){
HTMLDocument doc = (HTMLDocument)(super.createDefaultDocument());
doc.setAsynchronousLoadPriority(-1); // load synchronously
return doc;
}
}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!