Solved

Problem replacing DefaultStyledDocument with HTMLDocument in an Application

Posted on 2003-12-04
8
462 Views
Last Modified: 2008-02-01
Hi experts,

I'm trying to modify an application that uses JTextPane and DefaultStyledDocuments to implement an Editor.

I need to change the Documenttype from javax.swing.text.DefaultStyledDocument to
javax.swing.text.html.HTMLDocument in order to enhance the editor functionality.

As the documents that we want to edit are not being stored in files on a filesystem, but rather as a set of records in a database, we had to create a new kind of Document which now extends from the DefaultStyledDocument. so far so good ... everything works fine.

when i type a character the method "insertString(int offset, String str, AttributeSet a)" is being called, which we overload with our own database version, that calls the "super.insertString(...)" as well.

now when i change the DefaultStyledDocument to a HTMLDocument everything still works (more or less) .... then when i change the EditorKit to a HTMLEditorKit, the insertString(int offset, String str, AttributeSet a)"-Method is not being called anymore.

Typed text is still being inserted into the text area.... but our database-methods are no longer being called, as they are in the "insertString(...)"-Method that is never called anymore.

now my question : which method IS being called when i type a letter?? which method do i need to rewrite in order to hook my database calls in again everytime a letter is typed?

thanx for your help.

dominik

PS if this turn's out to be real tough one, i'm willing to increase points too.
0
Comment
Question by:dom_bus
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9874312
Are you sure the HTMLDocument you created is being used and not one got from the kit?
0
 
LVL 3

Expert Comment

by:savalou
ID: 9874369
Maybe you should derive from HTMLEditorKit to override the createDefaultDocument() method (if that's how you are creating your document, but also to test whether it's being called).  You could also then override insertHTML() to see what's going on.  Trace the calls in a debugger.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9874420
Make sure the same relationships are preserved as in the following:

   JEditorPane ep = new JEditorPane();
   ep.setContentType("text/html");
   getContentPane().add(ep);
   MyDoc myDoc = new MyDoc();
   ep.setDocument(myDoc);
   EditorKit kit = ep.getEditorKit();
   try {
         kit.read(new StringReader("<HTML><BODY>Type in some text<BR></BODY></HTML>"), myDoc, 0);
   }
   catch(Exception e) {
         e.printStackTrace();
   }


................


class MyDoc extends HTMLDocument {
      public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
            System.out.println("Called insertStr!");
                super.insertString(offset, str, a);
        }
}

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 9874477
(i.e. the relationship between doc, kit and pane). The above works fine for me.
0
 
LVL 92

Expert Comment

by:objects
ID: 9876991
can you post the relevant parts of your code and we'll help sort out what your problems are.
0
 

Author Comment

by:dom_bus
ID: 11662557
sorry for not accepting this answer (together with the above post of CEHJ) earlier.

lost track of it as soon i finally had this and some other problems solved.

best regards & thanx again.

dom_bus
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11665365
8-)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question