1

My code reads an HTML file and I want to show it in new frame as a web page. But I don't know - how can I do this?

This is my code:

public class EditorPaneLoad extends JFrame{ 

public EditorPaneLoad() throws Exception{

    FileReader reader = new FileReader("a.html");
    JEditorPane editor = new JEditorPane();
    JTextPane editor = new JTextPane();
    editor.setContentType( "text/html" );
    editor.setEditable( false );
    editor.read(reader, null);
    //System.out.println(editor.getText());
    //System.out.println("\n------------\n");
    Document doc = editor.getDocument();
    // System.out.println(doc.getText(0, doc.getLength()));
    JScrollPane scrollPane = new JScrollPane( editor );
    scrollPane.setPreferredSize( new Dimension(300, 200) );
    getContentPane().add( scrollPane );
}

public static void main(String[] args)
    throws Exception
{
    EditorPaneLoad frame = new EditorPaneLoad();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setLocationRelativeTo( null );
    frame.setVisible(true);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I don't understand. The current code should 'show HTML as HTML formatted'. What more do you expect? – Andrew Thompson Jan 13 '14 at 20:25
  • i want to make it in Design type GUI in netbeans with button,but i can't – user3188155 Jan 13 '14 at 20:28
  • Do you mean you want to make an HTML editor? Note that `JEditorPane` was never intended to display 'real world HTML'. It only supports HTML **3.2** and limited very styles. No applets, flash or JavaScript are supported at all. It certainly would not be useful for designing 'real world HTML'! – Andrew Thompson Jan 13 '14 at 20:31
  • no,i have a HTML file,and i want to Display it as a web-page,as a real page in Browser,but it just a HTML code,and i want it to read a HTML file and open a new Frame and in this frame show the web version of that HTML code,show me a example Code in Netbeans – user3188155 Jan 13 '14 at 20:35

2 Answers2

2

If I get it right, you want to render HTML through a desktop window of your application.

Perhaps flying saucer would help you. An alternative, Lobo would be rendered using javafx but it would only support HTML 4.

Hope I helped!

Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36
1
// opens "a.html" in the default browser..
Desktop.getDesktop().open(new File("a.html"));

See Desktop.open(File) for details.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @user3188155 This would run the default browser showing the specific html. It is not the same as showing the html through **your desktop application**. – Pantelis Natsiavas Jan 17 '14 at 17:30
  • @PantelisNatsiavas But note it will show it *"as a web-page,as a real page in Browser"* as per the OP's 2nd comment. – Andrew Thompson Jan 17 '14 at 23:09