HTMLEditorKit Demo : Document HTML « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Development Class » Document HTMLScreenshots 
HTMLEditorKit Demo
HTMLEditorKit Demo

/*
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 LoadSync {

  public static void main(String args[]) {
    final String filename = "Test.html";
    JFrame frame = new JFrame("Loading/Saving Example");
    Container content = frame.getContentPane();

    final JEditorPane editorPane = new JEditorPane();
    editorPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(editorPane);
    content.add(scrollPane, BorderLayout.CENTER);

    editorPane.setEditorKit(new HTMLEditorKit());

    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);

    frame.setSize(250150);
    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 = (HTMLDocumenthtmlKit
          .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 (true means to ignore character set)
      parser.parse(reader, callback, true);
      // Replace document
      textComponent.setDocument(htmlDoc);
      System.out.println("Loaded");

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

           
       
Related examples in the same category
1. HTMLDocument: Element Iterator Example
2. SimpleAttributeSet ExampleSimpleAttributeSet Example
3. Text Tab SampleText Tab Sample
4. Styled DocumentStyled Document
w__w__w___._j___a___va___2s___._c___o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.