问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/12145433/getting-indirect-source-of-documentlistener