Results 1 to 1 of 1

Thread: MVC Help!

  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation MVC Help!

    Greetings,

    I am about to study the concept of Observable and Observer API.
    But later in the study, I can't figure out on how to implement it on this project.
    I have included below the SSCCE of the project. (Though many, if the best I can do).

    The problem is the the MainController has this MainModel. With the code below, I can't figure out how both MainControllerOne and MainControllerTwo interact using the same MainModel created on MainController.

    Or in short, I can't figure out how the nullBox of MainFrame work on the both MainControllerOne and MainControllerTwo to check whether the MainModel(from MainController) is either null or not.

    Here's the code below. Or the its project(Netbeans 6.9.1) attached on this thread

    MainRun:
    Java Code:
    import javax.swing.SwingUtilities;
    
    public class MainRun {
    
        public static void main(String[] a) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    MainController mainController = new MainController();
                }
            });
        }
    
    }
    MainView:
    Java Code:
    import java.awt.Dimension;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    
    public class MainView extends JFrame {
    
        public JDesktopPane desktop = new JDesktopPane();
    
        public MainView() {
            super();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(new Dimension(800, 600));
            setVisible(rootPaneCheckingEnabled);
            add(desktop);
        }
    
    }
    MainController:
    Java Code:
    public class MainController extends MainView {
    
        public MainController() {
            mainControllerOne = new MainControllerOne(mainModel);
            mainControllerTwo = new MainControllerTwo(mainModel);
            desktop.add(mainControllerOne);
            desktop.add(mainControllerTwo);
        }
    
        private MainControllerOne mainControllerOne = null;
        private MainControllerTwo mainControllerTwo = null;
        private MainModel mainModel = null;
    
    }
    MainModel:
    Java Code:
    public final class MainModel {
    
        public MainModel(int id, String data) {
            setId(id);
            setData(data);
        }
    
        public MainModel(String id, String data) {
            setId(id);
            setData(data);
        }
    
        public String getData() {
            return data;
        }
    
        public void setData(String data) {
            this.data = data;
        }
    
        public int getId() {
            return id;
        }
    
        public String getIDString() {
            return String.valueOf(id);
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public void setId(String id) {
            this.id = Integer.parseInt(id);
        }
    
        private int id;
        private String data;
    
    }
    MainFrame:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class MainFrame extends JInternalFrame {
    
        public MainFrame() {
           super("Frame One");
           setSize(new Dimension(300, 200));
           setVisible(true);
           setLayout(new BorderLayout(5, 5));
           add(nullBox, BorderLayout.NORTH);
           add(loadPanel(), BorderLayout.CENTER);
           add(buttonPanel(), BorderLayout.SOUTH);
        }
    
        private JPanel loadPanel() {
            JPanel panel = new JPanel(new GridLayout(2, 2, 5, 5));
                panel.add(idLabel); panel.add(idField);
                panel.add(nameLabel); panel.add(nameField);
            return panel;
        }
    
        private JPanel buttonPanel() {
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
                panel.add(okButton); panel.add(cancelButton);
            return panel;
        }
    
        private final JLabel idLabel = new JLabel("ID");
        private final JLabel nameLabel = new JLabel("Name");
        public JCheckBox nullBox = new JCheckBox("IsNULL", false);
        public JTextField idField = new JTextField();
        public JTextField nameField = new JTextField();
        public JButton okButton = new JButton("Ok");
        public JButton cancelButton = new JButton("Cancel");
    
    }
    MainControllerOne:
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MainControllerOne extends MainFrame {
    
        public MainControllerOne(MainModel mainModel) {
            this.mainModel = mainModel;
            setTitle("Frame One");
            loadListener();
        }
    
        private void loadListener() {
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mainModel = new MainModel(idField.getText(), nameField.getText());
                    checkNull();
                }
            });
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mainModel = null;
                    checkNull();
                }
            });
        }
    
        private void checkNull() {
            nullBox.setSelected((mainModel == null ? true : false));
        }
    
        private MainModel mainModel;
    
    }
    MainControllerTwo:
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MainControllerTwo extends MainFrame {
    
        public MainControllerTwo(MainModel mainModel) {
            this.mainModel = mainModel;
            setTitle("Frame Two");
            loadListener();
        }
    
        private void loadListener() {
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mainModel = new MainModel(idField.getText(), nameField.getText());
                    checkNull();
                }
            });
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mainModel = null;
                    checkNull();
                }
            });
        }
    
        private void checkNull() {
            nullBox.setSelected((mainModel == null ? false : true));
        }
    
        private MainModel mainModel;
    }
    Attached Files Attached Files
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •