Using Actions with a DocumentListener

I am developing an application in which I want something to be launched both by a user updating the contents of JTextArea and manually by pressing the JButton button.

I completed the first part with a DocumentListener and put the appropriate code in my insertUpdate method.

I have not used Action before, but I heard that they are useful for situations where you need something to be triggered by multiple controls. Is it possible to trigger an action from a DocumentListener? Is it a good idea to use Actions in general, or should I just put my code in a regular method?

(in the constructor):

  textAreaInput.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { // do something } public void removeUpdate(DocumentEvent e) {} public void changedUpdate(DocumentEvent e) {} }); 

and Action, which is a field:

 Action doSomething = new AbstractAction("Do Something!") { @Override public void actionPerformed(ActionEvent e) { // do it } }; 

explanation:

JTextArea will get the text inserted by the user that I want to automatically parse. The parsing depends on other values ​​set elsewhere in the GUI; if the user changes these other values, he may want to re-parse the text, so you need to perform the same action by clicking the button.

+7
source share
3 answers

You can call the actionPerformed() method, whether it is in Action or not. Here is an example here .

+3
source

I want something to work both as a user updating the contents of JTextArea, and manually by pressing the JButton button.

That doesn't make sense to me.

Why does clicking a button cause the same action as a user entering text into a text area?

I have not used Actions before, but I heard that they are useful for situations when you need something to be triggered by several controls

This operator is for controls that the user clicks, such as JMenuItems, JButtons, or by pressing Enter in the text box. In general, they can be used when you use an ActionListner.

The DocumentListener is not an ActionListener, as I previously stated that using an action does not seem appropriate.

I think you need to clarify your requirements.

Change based on explanation

if the user changes these other values, he may want to reanalyze the text

Why does the user have a choice? If you change the font, text, foreground, background of the text area, the component that it automatically repaints, you do not need to ask about it. If you look at the code for these methods, they always call the revalidate () and repaint () methods.

The parsing depends on other values ​​set elsewhere in the GUI;

It looks like you need a custom class. Maybe ParsedTextArea or ParsedDocument. This class will contain "properties" that can be set elsewhere in the GUI. He will implement DocumentListener. It will also support your parseTheText method. Therefore, whenever a property is changed or a DocumentEvent document is created, you automatically call the parseTheText method. This way, you do not need a separate button, and the component will always be synchronized, because the parsing is automatic.

+4
source

I think you do not need to create an Action object. You can add an ActionListener to a button just like you added a DocumentListener to an input document. If I understand your problem correctly, perhaps you should do something like this:

 textInput.getDocument().addDocumentListener(new DocumentListener(){ @Override public void insertUpdate(DocumentEvent e) { doIt(); } @Override public void removeUpdate(DocumentEvent e) {} @Override public void changedUpdate(DocumentEvent e) {} }); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { doIt(); } }); 

doIt() is a method in which you will do what you want.

+2
source

All Articles