0

I have a DocumentFilter installed on a Document attached to JTextArea. The purpose of the filter is to handle and filter changes made by the user, and when they are detected, to update another (graphical) view of the document based on the document content (the document is a textual representation of the desired graphics).

I have a little problem of the filter to be called also when I perform setText on the JTextArea. In this situation I would like the filter not to be processed, as the input is not coming from the user, and the document is already known, the graphical view loaded and it is not necessary to load it again. I see some trivial options how to prevent it, but they all smell to me:

  • create a global Boolean variable telling the filter to suppress itself
  • disable the filter before the setText call and install it again once it is finished

I guess there must be some nicer way. Any advice?

Suma
  • 33,181
  • 16
  • 123
  • 191
  • A bit similar [Make parts of a JTextArea non editable (not the whole JTextArea!)](http://stackoverflow.com/questions/10030477/make-parts-of-a-jtextarea-non-editable-not-the-whole-jtextarea) – Suma Jan 08 '15 at 11:57
  • Why are you setting the text in the JTextArea? If the text is already known by the Document shouldn't the JTextArea update from the Document? – Samuel Åslund Jan 08 '15 at 12:12
  • @SamuelÅslund When user loads a new file, I need to update the document somehow - I did not find a way how to update it without the filter being involved. Based on the answer to the question I have linked in my comment it seems the solutions I have mentioned as trivial but smelling are common and there is no other way. Another (I think similar) way could be to create a new Document and attach it in that situation, and set the filter only after the document is fully loaded. – Suma Jan 08 '15 at 12:25
  • I would expect that you want the view to be updated when you read a document from disk also? So what stops you from doing the programmatic update directly to the Document? (Or if the JTextArea is broken using the "setText" as the only update?) – Samuel Åslund Jan 08 '15 at 12:36
  • @SamuelÅslund Even when I do update directly on the document (using replace method of Document), the filter is still called. Can you advise some way how to update the document without triggering a filter? – Suma Jan 08 '15 at 12:42
  • No, I do not see any way to avoid calling the filter, it seems like you can use a "WriteLock" to avoid potential races. But I still do not see why you need to get past it, the view is rendered by the program when you read the data, right? Do you really need to render the view before informing the Document about it? – Samuel Åslund Jan 08 '15 at 12:48

0 Answers0