ActionPerformed within MVC

0

I have a question about a project that I am doing, which is the following: I am being asked for a project with MVC format, I already have the view and the model, I need to perform the ActionPerformed of each button of my project, which are approximately 11 buttons of type JButton, I would like to know how I can do it in the project, just press the button and call the method of reading the file of each of the groups to show the information of it, I leave the linked code:

MODEL

/ *  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  * / model package;

import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JOptionPane;

/ **  *  * @author Diablo  * / public class russiaModel extends JFrame {

public void LeerGrupoA() throws FileNotFoundException, IOException{
    String grupoA;
    FileReader gA= new FileReader("grupoA.txt");
    BufferedReader a=new BufferedReader(gA);
    while((grupoA=a.readLine())!=null){
        JOptionPane.showMessageDialog(null,grupoA);
    }
   a.close();

}
public void LeerGrupoB() throws FileNotFoundException, IOException{
    String grupoB;
    FileReader gB= new FileReader("grupoB.txt");
    BufferedReader b=new BufferedReader(gB);
    while((grupoB=b.readLine())!=null){
        JOptionPane.showMessageDialog(null,grupoB);
    }
   b.close();
}
public void LeerGrupoC() throws FileNotFoundException, IOException{
    String grupoC;
    FileReader gC= new FileReader("grupoC.txt");
    BufferedReader c=new BufferedReader(gC);
    while((grupoC=c.readLine())!=null){
        JOptionPane.showMessageDialog(null,grupoC);
    }
   c.close();
}
public void LeerGrupoD() throws FileNotFoundException, IOException{
    String grupoD;
    FileReader gD= new FileReader("grupoD.txt");
    BufferedReader d=new BufferedReader(gD);
    while((grupoD=d.readLine())!=null){
        JOptionPane.showMessageDialog(null,grupoD);
    }
   d.close();
}
public void LeerGrupoE() throws FileNotFoundException, IOException{
    String grupoE;
    FileReader gE= new FileReader("grupoE.txt");
    BufferedReader e=new BufferedReader(gE);
    while((grupoE=e.readLine())!=null){
        JOptionPane.showMessageDialog(null,grupoE);
    }
   e.close();
}
public void LeerGrupoF() throws FileNotFoundException, IOException{
    String grupoF;
    FileReader gF= new FileReader("grupoF.txt");
    BufferedReader f=new BufferedReader(gF);
    while((grupoF=f.readLine())!=null){
        JOptionPane.showMessageDialog(null,grupoF);
    }
   f.close();
}
public void LeerGrupoG() throws FileNotFoundException, IOException{
    String grupoG;
    FileReader gG= new FileReader("grupoG.txt");
    BufferedReader g=new BufferedReader(gG);
    while((grupoG=g.readLine())!=null){
        JOptionPane.showMessageDialog(null,grupoG);
    }
   g.close();
}
public void LeerGrupoH() throws FileNotFoundException, IOException{
    String grupoH;
    FileReader gH= new FileReader("grupoH.txt");
    BufferedReader h=new BufferedReader(gH);
    while((grupoH=h.readLine())!=null){
        JOptionPane.showMessageDialog(null,grupoH);
    }
   h.close();
}

}

CONTROLLER

  /*

* To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  * / controller package;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashSet; import model.russiaModel; import vista.Vista;

public class Controller Implements ActionListener {

private Vista view;
private rusiaModelo model;

public Controlador(Vista view, rusiaModelo model){
    this.view= view;
    this.model=model;
    this.view.botonGrupoA.addActionListener(this);
    this.view.botonGrupoB.addActionListener(this);
    this.view.botonGrupoC.addActionListener(this);
    this.view.botonGrupoD.addActionListener(this);
    this.view.botonGrupoE.addActionListener(this);
    this.view.botonGrupoF.addActionListener(this);
    this.view.botonGrupoG.addActionListener(this);
    this.view.botonGrupoH.addActionListener(this);

}
public void iniciar()
{
    view.setTitle("Mundial Rusia");
    view.setLocationRelativeTo(null);
 }

@Override
public void actionPerformed(ActionEvent e){



}

}

I hope you can help me solve my problem ...

PDTA: Try with an If (buttonGrupoA.isSelected) { model.LeerA (); } But it did not work ... Thank you very much for the answer in advance.

    
asked by Diego Alejandro 30.05.2018 в 22:59
source

2 answers

0

I give the subject finished, I could solve the error I had in reading files and the ActionPerformed of each button created, many thanks to the people who responded.

    
answered by 31.05.2018 в 01:03
-1

In theory the getSource () method of an event (ActionEvent) gives you where the event originated. If you use JButton then it must give you the object that triggered the event. Seeing how you add the listeners in the Controller class, this should work:

@Override
public void actionPerformed(ActionEvent e){
    Object origen = e.getSource();
    try {
       if (origen == this.view.botonGrupoA) { leeGrupoA(); }
       else if (origen == this.view.botonGrupoB) { leeGrupoB(); }
       //etcetera
    }
    catch (Exception ex) {
       ///lo que quieras hacer con tu excepción
    }
}

I'm adding the try-catch because I see that your methods throw exceptions.

    
answered by 31.05.2018 в 00:21