When we keep the code organized, with correct indentation becomes easier to understand:
ActionListener trataEventos = new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
conteudo += e.getActionCommand();
campoDoFormularioHTML.setMember("value", "" + conteudo);
}
};
I put in the Github for future reference.
Note that the last key closes the beginning of the method ActionListener()
. Every block of code that is opened needs to be closed.
The semicolon is just closing a common line, as is always done. Note that the code block is part of only one line of code started in ActionListener trataEventos = new ActionListener() {
. This is a line that declares a variable and assigns a value to it. In this case this value is a code block of a anonymous class (class will only exist there and cannot be used as type elsewhere). Within this class you have the definition of a method.
In this case it is necessary because it is creating an event. It is a way of implementing the observer standard (another question). It is something that needs to react to something that happens in the application. This something is a concrete action that must be defined by some code, a method, so it creates a class that contains this method that will be called when the event occurs. Java 8 introduced a new resource that avoids creating a class just for this.
I’m going to write a code here that is not correct but it would help to understand what happens in that only code line:
class ClasseTemporaria implements ActionListener {
public void actionPerformed(java.awt.event.ActionEvent e) {
conteudo += e.getActionCommand();
campoDoFormularioHTML.setMember("value", "" + conteudo);
}
}
ActionListener trataEventos = new ClasseTemporaria();
I will not go into more detail because this subject is a little advanced and by the history of the AP I think more information to explain the anonymous class resource and the specific use, in this case, before understanding other more basic things will only hinder.
Explain better, what parentheses are you talking about?
– Maniero
This is because you are making a call to an anonymous class when you gave a new in Actionlistener.
– user28595
Anonymous class that exists?
– Aline
Nested classes: what they are and when to use?
– user28595
Anonymous classes, they have their reason to be but in java have always been used most of the time to plug the hole that was the lack of support to Amble before version 8.
– BrunoRB
Okay @Diegof I will study this. D
– Aline