Getting indirect source of DocumentListener

匆匆过客 提交于 2019-12-24 09:26:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!