MVC and JAVA doubt

Asked

Viewed 101 times

1

Giving continuity to a project of the college that was stopped, I intend to do it in Java already I did and I remade from scratch about 2 times however, it was horrible and to each class I realized that I could do better, so I decided to try to organize the code using MVC.

My question is, why is this code not working? It’s been about three days that I’m reading the stackoverflow and so far I haven’t found the problem.

I’m using this tutorial as a basis: http://www.gqferreira.com.br/artigos/ver/mvc-com-java-desktop-parte3

Model

     package duvidamvc;

    import javax.swing.JOptionPane;

    public class DuvidaModel {
        // Construtor
        public DuvidaModel() {
            iniciar();
        }

        public void iniciar() {
            JOptionPane.showMessageDialog(null, "Funcionou.");
        }
    } //aqui tinha uma chave sobrando 

Controller

   package duvidamvc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DuvidaController implements ActionListener {
    private DuvidaView view;
    private DuvidaModel modelo;

    // Construtor
    public DuvidaController(DuvidaView view) {
        this.view = view;
        this.view.getBtUm().addActionListener(this);
        this.view.getBtDois().addActionListener(this);
        this.view.getBtTres().addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.view.getBtUm()) {
            chamandoModel();
        }
        if (e.getSource() == this.view.getBtDois()) {
            chamandoModel();
        }
        if (e.getSource() == this.view.getBtTres()) {
            chamandoModel();
        }
    }

    private void chamandoModel() {

        modelo = new DuvidaModel();
        modelo.iniciar();
    }
}

View

    package duvidamvc;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class DuvidaView extends JFrame {
        // Encapsulamento
        private JFrame tela;
        private JPanel painel;
        private JButton btUm;
        private JButton btDois;
        private JButton btTres;

        // Construtor
        public DuvidaView() {
            janela();
        }

        public JButton getBtUm() {
            return btUm;
        }

        public JButton getBtDois() {
            return btDois;
        }

        public JButton getBtTres() {
            return btTres;
        }

        public void janela() {
            JFrame tela = new JFrame();
            painel = new JPanel();
            // painel.setLayout(null);
            tela.setTitle("Duvida padrão MVC");
            tela.setDefaultCloseOperation(EXIT_ON_CLOSE);
            tela.add(painel);
            tela.setSize(400, 80);
            btUm = new JButton("Exemplo 1");
            painel.add(btUm);
            btDois = new JButton("Exemplo 2");
            painel.add(btDois);
            btTres = new JButton("Exemplo 3");
            painel.add(btTres);
            tela.setVisible(true);
            pack();
        }

        public static void main(String[] args) {
            new DuvidaView();
        }
    }
  • Place four spaces before each line of code to format it as code. Or select your code and click the button with the {}.

  • James, what is the error that is being displayed in your IDE?

  • The IDE doesn’t make any mistakes at all, that’s the problem

1 answer

4


First, if you put one JOptionPane on your model, you are already violating MVC. Swing components are things from the view. Do not put anything referencing any user interface in the model.

Second, that you should only have one model. But how are you instantiating the model within the method chamandoModel and this same method ends up dropping the model to the garbage collector, once again you violated the MVC. The controller is used to paste the view into the model and not to create and recreate the model whenever an action is executed. The field private DuvidaModel modelo; which is what you should probably use, you’re not using.

Third, that DuvidaController implements ActionListener It’s bad programming practice. Plus you play control of all buttons in the same method to then separate with a lot of ifs, which is another bad programming practice. Give a method to each button and do not mix them.

Fourth, do not violate the view encapsulation in the controller. The view should warn the controller, not the controller access the view components to inject itself there.

Fifth, or you use extends JFrame or else you create the JFrame with new. The two together don’t make sense.

So your code could be this:

package duvidamvc;

public class DuvidaModel {

    public DuvidaModel() {
    }

    public String fazerAlgumaCoisa(int n) {
        return "Funcionou " + n + ".";
    }
}
package duvidamvc;

public class DuvidaController {
    private final DuvidaView view;
    private final DuvidaModel modelo;

    public DuvidaController(DuvidaModel modelo) {
        this.modelo = modelo;
        this.view = new DuvidaView(this);
    }

    public String clicouBotao1() {
        return modelo.fazerAlgumaCoisa(1);
    }

    public String clicouBotao2() {
        return modelo.fazerAlgumaCoisa(2);
    }

    public String clicouBotao3() {
        return modelo.fazerAlgumaCoisa(3);
    }
}
package duvidamvc;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DuvidaView {

    public DuvidaView(DuvidaController controller) {
        JFrame tela = new JFrame();
        painel = new JPanel();
        //painel.setLayout(null);
        tela.setTitle("Duvida padrão MVC");
        tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tela.add(painel);
        tela.setSize(400, 80);

        btUm = new JButton("Exemplo 1");
        painel.add(btUm);
        btUm.addActionListener(e -> JOptionPane.showMessageDialog(null, controller.clicouBotao1()));

        btDois = new JButton("Exemplo 2");
        painel.add(btDois);
        btDois.addActionListener(e -> JOptionPane.showMessageDialog(null, controller.clicouBotao2()));

        btTres = new JButton("Exemplo 3");
        painel.add(btTres);
        btTres.addActionListener(e -> JOptionPane.showMessageDialog(null, controller.clicouBotao3()));

        tela.setVisible(true);
        tela.pack();
    }
}
package duvidamvc;

public class Principal {
    public static void main(String[] args) {
        DuvidaModel m = new DuvidaModel();
        new DuvidaController(m);
    }
}
  • I am based only on the article in the link below, because I did not find any book or anything that would add with practical examples the use of MVC with java, in my view it seems that each developer implements the way it thinks correct, so much so that when I questioned this in another community I was told "MVC is not a law", so this comment gave me to realize that actually each one does the way they like taking the idea only of the concept that someone played somewhere at some point on the internet.

  • Could you recommend some content on this subject with some examples or some simple project that makes me better understand the concept?

  • @James https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

  • @James There is no single way, only one way to make MVC. What exists are concepts and principles that must be respected and objectives that must be achieved.

  • @James The most important thing is to separate the model from the view. The model is responsible for storing business rules and should not be polluted with visualization logic. If your model is done correctly, if you change your swing system to a JSF, or Javafx, or console, or webservice, or whatever, you should not/should change a single comma of your model. On the other hand the view should contain only and only visualization logic and be dumb, leaving all the system intelligence only in the model.

  • @James After you separate the model view properly, something is still missing so that you can talk to each other, and that’s where the controller comes in.

  • I must leave the listeners in the view or controller?

  • @James Preferably in the view.

  • so if I ever change my system to Javafx or console for example, I should just change the view and controller or just the view?

  • @James Ideally just the view, but most of the time, you’ll have to change the controller too, which is also fine. The problem is if you have to change the model.

Show 5 more comments

Browser other questions tagged

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