views:

219

answers:

3

I'm writing a Java TextComponent where the underlying document has some structure. It is very short, basically one line. I need to be able to override the response to inserting or deleting characters in some parts of the document.

My initial approach was to implement javax.swing.text.Document, but this seems to involve developing many associated classes (Element, EditorKit, View) and there don't seem to be many examples or tutorials on how to do this. There are plenty on using the pre-defined implementations.

Can anyone point me at tutorials or other aids for simple implementations of Document and what other classes I need to create to make this work.

+3  A: 

There is a Java Swing tutorial on using text components.

Basically speaking you can add your DocumentListener to the document and react to changes or write your own implementation of document. Using AbstractDocument that should be easy, because it has only a couple of abstract methods you need to implement. Alternatively you may alos subclass PlainDocument and only override a certaing set of methods of interest to you.

Given your constraints, that you only need to react somehow on insertion/deletion of characters adding a listener on the document should work best.

Let me know if you need more details.

pajton
The `DocumentListener` solution is correct - however, there is one caveat if you think about reacting to inserts/deletes by changing the document itself: you can't do that. That is, you're not allowed to change the document within one of the `DocumentListener` methods. If you need to do this, wrap your code in a Runnable and put it in the Swing event queue by calling SwingUtitities.invokeLater() on it.
Thomas
I know about that tutorial, but I don't think it's not detailed enough for my purposes. I found myself having to implement Element, and possibly View and ViewFactory, which are only discussed very briefly. Do we know of anything else?
DJClayworth
Ok, how about subclassing `PlainDocument`? Just override desired methods i.e. `insertString` to react on inserts `removeUpdate` to react on deletes etc. Just make sure to call super methods at some point.@Thomas Are you sure? I believe that listeners are notified on Event Dispatching Thread...
pajton
DocumentListener didn't do the right thing, because it reacts after the change. DocumentFilter prevents changes getting to the document if required.
DJClayworth
+2  A: 

If you just want to react to character insertion/deletion then maybe a DocumentFilter is the better approach. This will allow you to prevent the insertion of characters to the Document is you so desire. Read the JTextField API and follow the link to the Swing tutorial on "Text Component Features" for more information.

camickr
The DocumentFilter turned out to be viable. It wasn't the best solution, because I have to parse the string stored in the document into its components every time a change it made, but constructing your own document seems to be far too complicated.
DJClayworth
A: 

There is EditorKit – Document – Views relations tutorial, which gives some insight into implementation of custom javax.swing.text.Document.

Andrej Herich