JEditorPane and HTML


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JEditorPane and HTML

  1. #1
    Join Date
    Feb 2004
    Posts
    31

    JEditorPane and HTML

    Hi all,

    I would like to be able to pass to a JEditorPane a bit of dynamically created HTML which is just a few lines in the Header and an image. But when I run the program, the JEditorPane shows exactly the String that I pass in:
    <html>
    <body>
    etc.

    Is there a simple way that I can tell the JEditorPane to display the HTML (without writting about 100+ lines of code) or would I be better off to write the String as a temporary HTML file and then point the JEditorPane to the temporary file.

    Thanks in advance for your time.

    James D.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here is a simple HTML 'editor'

    It includes a tabbed pane w. a html textPane (uneditable) and a plain text
    pane (editable), and an undo button.
    The code does not refresh the hml when it is changed in the plain text
    panel though. Refer to the Sun java tutorial for including images in the
    display:

    http://java.sun.com/docs/books/tutor...ents/text.html

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    import javax.swing.undo.*;
    import java.util.*;
    import java.net.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.event.UndoableEditEvent;
    import javax.swing.event.UndoableEditListener;
    
    
    /**
     * Simple HTML 'editor'
     * @author sjalle
     * @version 1.0
     */
    
    public class HTMLEditor extends JFrame implements ActionListener, DocumentListener, UndoableEditListener {
      BorderLayout borderLayout1 = new BorderLayout();
      JPanel jPanel1 = new JPanel();
      JButton getFileBtn = new JButton();
      JButton closeBtn = new JButton();
      JTabbedPane jTabbedPane1 = new JTabbedPane();
      JScrollPane jScrollPane1 = new JScrollPane();
      JScrollPane jScrollPane2 = new JScrollPane();
      JEditorPane edText = new JEditorPane();
      JEditorPane edHTML = new JEditorPane();
      Document textDoc=null;
      ArrayList undoList=new ArrayList();
      JButton undoBtn = new JButton();
    
      public HTMLEditor() {
        try {
          jbInit();
          getFileBtn.addActionListener(this);
          undoBtn.addActionListener(this);
          closeBtn.addActionListener(this);
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public void loadFile(String fp) throws Exception {
    
        if (fp==null) return;
        String filePath=fp;
        File f=new File(fp);
        long av=f.length();
        if (av==0) {
          System.out.println("no data");
          return;
        }
        URL url=new URL("file:"+fp);
        edHTML.setPage(url);
        FileReader fr=new FileReader(f);
        edText.read(fr,null);
    
        textDoc=this.edText.getDocument();
        textDoc.addDocumentListener(this);
        textDoc.addUndoableEditListener(this);
    
        this.validate();
      }
    
      public static void main(String[] args) {
        HTMLEditor ed = new HTMLEditor();
        ed.setBounds(20,20,500,400);
        ed.setVisible(true);
      }
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        getFileBtn.setText("Get FIle");
        closeBtn.setText("Close");
        edText.setText("jEditorPane1");
        edHTML.setEditable(false);
        edHTML.setText("jEditorPane2");
        undoBtn.setEnabled(false);
        undoBtn.setText("Undo");
        this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
        jPanel1.add(getFileBtn, null);
        jPanel1.add(undoBtn, null);
        jPanel1.add(closeBtn, null);
        this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
        jTabbedPane1.add(jScrollPane1,   "HTML");
        jScrollPane1.getViewport().add(edHTML, null);
        jTabbedPane1.add(jScrollPane2,   "Text");
        jScrollPane2.getViewport().add(edText, null);
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
      }
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.closeBtn)
          System.exit(0);
        if (e.getSource() == this.getFileBtn) {
          FileDialog fd=new FileDialog(this,"Select File",FileDialog.LOAD);
          fd.setVisible(true);
          String fName=fd.getFile();
          if (fName==null) return;
          String fPath=fd.getDirectory()+fName;
          try {
            loadFile(fPath);
          }
          catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
          }
        } else if (e.getSource() == this.undoBtn) {
          undoLast();
        }
      }
      public void undoLast() {
        int ix=undoList.size()-1;
        if (ix>=0) {
          UndoableEdit ue=(UndoableEdit)this.undoList.get(ix);
          ue.undo();;
          undoList.remove(ix);
          if (undoList.size()==0) this.undoBtn.setEnabled(false);
        }
      }
      /**
       * If the undoList has any elements then the document has
       * been changed
       * @return
       */
      public boolean isFileChanged() {
        return this.undoList.size()>0;
      }
      public void insertUpdate(DocumentEvent e) {
        System.out.println("insertUpdate");
      }
      public void removeUpdate(DocumentEvent e) {
        System.out.println("removeUpdate");
      }
      public void changedUpdate(DocumentEvent e) {
        System.out.println("changedUpdate");
      }
      public void undoableEditHappened(UndoableEditEvent e) {
        this.undoList.add(e.getEdit());
        this.undoBtn.setEnabled(true);
      }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center