1

I have a document listener, and it's indirect source, JTextArea (JTextArea.getDocument().addDocumentListener etc..).

I need my listener to have access to JTextArea, but without referencing. Something similar to event.getSource() from action and key listeners... I'm aware that the source of the listener is the document, not JTextArea itself, but I need to get to it.

Any suggestions?

John Smith
  • 19
  • 4

1 Answers1

2

A Document is the model of a text component, and Swing is built for a single model to be able to be used by many views. So the answer to your question as far as I know of is no, this can't be done directly, since many text components can use the same Document.

You state:

It should remove all the excess text(copy/paste have to be taken into consideration too) and a dialog should popout informing user of the limitation. I am still unsure how to achieve that(having copy/paste in mind, without it it can be done easy), so if you have ideas shoot, but I'll need this argument issue solved anyway...

I wonder if you need that much control over input into a JTextArea, perhaps you shouldn't be using a JTextArea. Instead perhaps you should us a JList and completely control how lines are added and removed from this component.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Can I pass JTextArea as an additional argument to the listener? – John Smith Aug 27 '12 at 16:14
  • @John: sure you can. But you stated in your question that you didn't want to use references. I'm curious as to your motivation for all of this. Why do you need the JTextArea? – Hovercraft Full Of Eels Aug 27 '12 at 16:15
  • Document listener is supposed to make sure that no more than word wrapped lines of text exist in JTA, and I will put in row number detection in there and upon every height(or row number) change, it will have to run a method that checks that all of the component are still within their bounds (JTextArea is just one of many inside a JPanel with box layout). – John Smith Aug 27 '12 at 16:21
  • What will happen if the row numbers are exceeded? – Hovercraft Full Of Eels Aug 27 '12 at 16:28
  • It should remove all the excess text(copy/paste have to be taken into consideration too) and a dialog should popout informing user of the limitation. I am still unsure how to achieve that(having copy/paste in mind, without it it can be done easy), so if you have ideas shoot, but I'll need this argument issue solved anyway... – John Smith Aug 27 '12 at 16:32
  • @JohnSmith: HFOE is right; it's [_possible_](http://stackoverflow.com/a/8302423/230513) but not ideal. You may want to look at `DocumentFilter` or `InputVerifier`. – trashgod Aug 27 '12 at 17:56