开发者

Using Actions with DocumentListener

I'm developing an application where I want something to be triggered both by the user updating the contents of a JTextArea, or manually via pressing a JButton.

I have done the first part using a DocumentListener and putting the relevant code in its insertUpdate method.

I haven't used Actions before, but I've heard they are useful for situations wh开发者_开发技巧ere you need something to be triggered by multiple controls. Is it possible to trigger the action from the DocumentListener? Is it a good idea to use Actions at all, or should I just put my code in a normal 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 the Action, which is a field:

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

clarification:

The JTextArea will receive text that is pasted in by the user, which I want to parse automatically. 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, hence the need to perform the same action by pressing a button.


I want something to be triggered both by the user updating the contents of a JTextArea, or manually via pressing a JButton.

This doesn't make sense to me.

Why would clicking a button invoke the same Action as a user typing text into a text area?

I haven't used Actions before, but I've heard they are useful for situations where you need something to be triggered by multiple controls

That statement is meant for controls that the user clicks, like JMenuItems, JButtons or hitting Enter on a text field. In general they can be used when you use an ActionListner.

A DocumentListener is not an ActionListener so as I stated earlier the use of an Action doesn't seem appropriate.

I think you need to clarify your requirement.

Edit, based on clarification

if the user changes these other values, he may want to re-parse the text

Why does the user have a choice? If you change the font, text, foreground, background of a text area, the component it automatically repainting, you don't have to ask for this to be done. If you look at the code for these methods they always end up invoking the revalidate() and repaint() methods.

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

Sounds like you need a custom class. Maybe a ParsedTextArea or ParsedDocument. This class would contain the "properties" that can be set elsewhere in the GUI. It would implmenent the DocumentListener. It would also support your "parseTheText" method. So whenever a property is changed or a DocumentEvent is generated you automatically invoked the "parseTheText" method. This way you don't need a separate button and the component will always be in sync because the parsing is automatic.


You can invoke the actionPerformed() method, whether it's in an Action or not. There's an example here.


I think you need not create the Action object. You can add ActionListener to the Button just like you have added DocumentListener to the Document of the input. If I correctly understand your problem, may be 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 wanted to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜