i need help with building the controller by using MVC pattern

view 1

import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;      
import javax.swing.*;  

    public class BankView {  
        JFrame frame;  
        JPanel one, two;  
        BankSaving saving = new BankSaving(); 
         BankCurrent current = new BankCurrent();


           BankView(){  
            frame = new JFrame();  

            one = new JPanel();  


            JButton b1 = new JButton("Add Account");  
            b1.addActionListener( new ActionListener() {  

                @Override  
                public void actionPerformed(ActionEvent arg0) {  
                    frame.setContentPane(two);  
                    two.revalidate();  
                }  
            });  

            JButton b2 = new JButton("Display");  
            b2.addActionListener( new ActionListener() {  

                @Override  
                public void actionPerformed(ActionEvent arg0) {  
                    frame.setContentPane(one);  
                   two.revalidate();  
                }  
            });

            JButton b3 = new JButton("Exit");  
            b3.addActionListener( new ActionListener() {  

                public void actionPerformed(ActionEvent e)  
              {
                System.exit(0);
              }
            });
            one.add(b1);  
            one.add(b2);
            one.add(b3);

            two = new JPanel();  

            JButton b4 = new JButton("Saving Account");  
             b4.addActionListener( new ActionListener() {  

                @Override  
                public void actionPerformed(ActionEvent arg0) {  
                    frame.setContentPane( saving.getContentPane()); 
                    two.revalidate();    }
            });  
            JButton b5 = new JButton("Current Account");  
            b5.addActionListener( new ActionListener() {  

                 @Override  
                public void actionPerformed(ActionEvent arg0) {  
                    frame.setContentPane(current.getContentPane());
                    two.revalidate();  
                }  
            });  


            two.add(b4);  
            two.add(b5);

            frame.setContentPane(one);  
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            frame.pack();  
            frame.setVisible(true);              
        }  



    }  

view saving

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class BankSaving extends JFrame
{
    private static final int WIDTH = 500;
    private static final int HEIGHT = 400;

    private JLabel icL, nameL, addressL,dobL,openL,accL,balanceL;
    private JTextField icTF, nameTF, addressTF,dobTF,openTF,accTF,balanceTF;
    private JButton inputB, exitB;

    //Button handlers:
    private ExitButtonHandler ebHandler;

    public BankSaving()
    {
        icL = new JLabel("Customer IC No: ", SwingConstants.RIGHT);
        nameL = new JLabel("Customer Name: ", SwingConstants.RIGHT);
        addressL = new JLabel("Customer Address: ", SwingConstants.RIGHT);
        dobL = new JLabel("Date of Birth: ", SwingConstants.RIGHT);
        openL = new JLabel("Date of Account Opening: ", SwingConstants.RIGHT);
        accL = new JLabel("Account No: ", SwingConstants.RIGHT);
        balanceL = new JLabel("Balance: ", SwingConstants.RIGHT);

        icTF = new JTextField(10);
        nameTF = new JTextField(10);
        addressTF = new JTextField(10);
        dobTF = new JTextField(10);
        openTF = new JTextField(10);
        accTF = new JTextField(10);
        balanceTF = new JTextField(10);


        inputB = new JButton("Save");

        exitB = new JButton("Exit");
        ebHandler = new ExitButtonHandler();
        exitB.addActionListener(ebHandler);


        Container pane = getContentPane();
        pane.setLayout(new GridLayout(8,2));

        //panel menu
        pane.add(icL);
        pane.add(icTF);
        pane.add(nameL);
        pane.add(nameTF);
        pane.add(addressL);
        pane.add(addressTF);
        pane.add(dobL);
        pane.add(dobTF);
        pane.add(openL);
        pane.add(openTF);
        pane.add(accL);
        pane.add(accTF);
        pane.add(balanceL);
        pane.add(balanceTF);
        pane.add(inputB);
        pane.add(exitB);

        setSize(WIDTH, HEIGHT);
        setVisible(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }



        public String geticTF() {
                return icTF.getText();
        }

        public String getnameTF() {
                return nameTF.getText();
        }


        public String getaddressTF() {
                return dobTF.getText();
        }


        public String getdobTF() {
                return dobTF.getText();
        }


        public String getopenTF() {
                return openTF.getText();
        }

        public String getaccTF() {
                return accTF.getText();
        }

        public String getbalanceTF() {
                return balanceTF.getText();
        }

    public class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }
}

controller

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BankController {
        private BankModel model;
        private BankSaving view;
        private BankCurrent vieww;


        public BankController(BankModel m, BankSaving s, BankSaving c) {
                this.model = m;
                this.view = s;
                this.view = c;
        }

    } 

im stuck here, how i suppose to build a controller?

It will depends on how you define controller... To me, a controller is a layer between model and view. Model deals purely with database calls (read & write). View deals with data that does not call for database but display what the data is (its data structure may be completely different from the database). The controller would use the model, create/build a data structure for the view, and pass that data to the view. The view can also pass parameters/arguments to the controller. Not sure what your definition is...

PS: Look at your view class to see what it needs for its display. Build/create that and store it back to the view class, so it can be displayed.

Edited 4 Years Ago by Taywin

i want to store the input to the model, but confuse how to make the controller to do that

here is my model

import java.util.ArrayList;
import java.util.Iterator;

public class BankModel {
        private String ic;
        private String name;
        private String address;
        private String dob;
        private String open;
        private String min;
        private String acc;
        private String balance;
        private ArrayList BankSaving = new ArrayList();
         private ArrayList BankCurrent = new ArrayList();

        public String geticTF() {
                return ic;
        }

        public String getnameTF() {
                return name;
        }

         public String getaddressTF() {
                return address;
        }

         public String getdobTF() {
                return dob;
        }

        public String getopenTF() {
                return open;
        }

        public String getminTF() {
                return min;
        }

        public String getaccountTF() {
                return acc;
        }

        public String getbalanceTF() {
                return balance;
        }

        public void setic(String newic) {
                ic = newic;
        }

        public void setname(String newname) {
                name = newname;
        }

        public void setaddress(String newaddress) {
                address = newaddress;
        }

        public void setdob(String newdob) {
                dob = newdob;
        }

         public void setopen(String newopen) {
                open = newopen;
        }

         public void setmin(String newmin) {
                min = newmin;
        }

        public void setaccount(String newaccount) {
                acc = newaccount;
        }

        public void setbalance(String newbalance) {
                balance = newbalance;
        }


        private boolean isEmptyString(String input) {
                return ((input == null) || input.equals(""));
        }


}

after input..i want to display it, thats all

This article has been dead for over six months. Start a new discussion instead.