Question

Je développe une application où je veux quelque chose à la fois être déclenché par l'utilisateur de mettre à jour le contenu d'un JTextArea, ou manuellement par pression sur une JButton.

Je l'ai fait la première partie à l'aide d'un DocumentListener et de mettre le code correspondant dans sa méthode insertUpdate.

Je ne l'ai pas utilisé Actions avant, mais je l'ai entendu dire qu'ils sont utiles pour les situations où vous avez besoin de quoi être déclenché par de multiples contrôles. Est-il possible de déclencher l'action de la DocumentListener? Est-ce une bonne idée d'Actions d'utilisation du tout, ou devrais-je mettre juste mon code dans une méthode normale?

(dans le constructeur):

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

et l'action, qui est un champ:

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

Précision:

Le JTextArea recevra le texte qui est collé par l'utilisateur, que je veux analyser automatiquement. L'analyse dépend d'autres valeurs définies ailleurs dans l'interface graphique; si l'utilisateur change ces valeurs, il peut vouloir ré-analyser le texte, d'où la nécessité d'effectuer la même action en appuyant sur un bouton.

Était-ce utile?

La solution

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

Autres conseils

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.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top