Doubt in the implementation of Actionlistener

Asked

Viewed 166 times

1

I have a college project to make a system of any category that, in this case, I chose a medical system and had something I didn’t quite understand about what the implementation would be like:

I have a JInternalFrame called Medical record with various text fields for information and buttons like Save Registration, Delete Registration etc....

A class called MedicoListener where all class actions would be implemented CadastroMedicos in a single ActionListener, where he would see which button was pressed and perform such action,).

How would the correct implementation of this?

Follows the Medicolistener class:

public class MedicoListener implements ActionListener {

Medico med = new Medico();
private CadastroMedico frame;

public MedicoListener(CadastroMedico frame){
    this.frame = frame;

}

@Override
public void actionPerformed(ActionEvent e) {

    //SALVAR


    //EXCLUIR

}
  • What don’t you understand? How it works to event orientation, how to separate operations in the same actionPerformed, the logic of a record-saving and deletion algorithm, or all the above?

  • How’s your class Medical records?

  • @Giuliana I didn’t understand how to separate actions in actionPerformed within the class I quoted

  • @diegofm The Registration class contains the Jframe for the user to fill in

  • So, can you add how it is? Or the chunk named after the variables of the buttons you need to filter.

  • @diegofm So, on this screen I have Jbuttonnovo, Jbuttonsalvar and Jbuttonexcluir, to facilitate the exemplification let’s assume that I have only the fields code and name for the register...

Show 1 more comment

2 answers

1


You can define "Actions Commands" to the buttons and then filter inside the Listener through a switch:

@Override
public void actionPerformed(ActionEvent e) {
   String ActCmd = e.getActionCommand();

   switch(actCmd){

  case "SALVAR":
     //aqui você faz a lógica do salvar
     break;
  case "EXCLUIR":
     //logica do excluir

   }

}

And on buttons, you need to make a small change by adding command actions similarly below:

JButtonSalvar.setActionCommand("SALVAR");
JButtonExcluir.setActionCommand("EXCLUIR");

The above change needs to be made in the build screen class, if you are using the netbeans GUI-Builder, just add after the method initComponents() in the class builder.

As a suggestion, create separate methods to make such steels (separate method for saving and deleting), if they are complex, and just call the specific method inside the switch...case so that the code is more organized.

  • 1

    Thank you very much! Well done!

  • Yes! I needed an initial idea, but now I understood perfectly... I have already marked hehe

0

For you to know which button triggered the event, you can use the getSource of ActionEvent:

String comando = ((JButton) e.getSource()).getText();
if (isComandoSalvar())
   //Chamar comando salvar
else
   //Chamar comando remover

Thus, based on the label of the button that triggered the event it will be possible to identify which operation to call, whether it will be save or deletion of the record. You could retrieve this information from another attribute of JButton, the important thing here is to understand that the information of the component that triggers the event is encapsulated in the ActionEvent and is accessible by the getSource.

  • It worked too, thank you Giuliana!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.