Results 1 to 3 of 3
  1. #1
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default [SOLVED] MVC question

    Hello I am rewriting my Programming editor by the MVC Model

    Java Code:
    public class MainEditor {
        //Create Model, View and Controller
        
        public static void main(String[] args) {
            EditorModel model = new EditorModel();
            EditorView view = new EditorView(model);
            EditorController controller = new EditorController(view, model);
            
            view.setVisible(true);
        }
    
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.lang.*;
    public class EditorView extends JFrame{
        //Components
        private JMenuBar mbar = new JMenuBar();
        private JToolBar tbar = new JToolBar();
        private JTextArea area = new JTextArea();
        private JTabbedPane tabbedpane = new JTabbedPane();
        private JScrollPane scrollpane = new JScrollPane(area);
        
        // Menu's on Menubar
        private JMenu filemenu = new JMenu("File");
        private JMenu editmenu = new JMenu("Edit");
        private JMenu helpmenu = new JMenu("Help");
        
        //Menu Items in the menu's on the MenuBar
        private JMenuItem newfile = new JMenuItem("New File");
        private JMenuItem openfile = new JMenuItem("Open File");
        private JMenuItem savefile = new JMenuItem("Save File");
        private JMenuItem savefileas = new JMenuItem("Save File As");
        private JMenuItem about = new JMenuItem("About");
        
        private EditorModel e_model; 
        
        public EditorView(EditorModel model){
            //Set up the logic
            e_model = model;
            
            // Layout Components
            getContentPane().add(tbar,BorderLayout.NORTH);
            getContentPane().add(tabbedpane,BorderLayout.CENTER);
            setJMenuBar(mbar);
            mbar.add(filemenu);mbar.add(editmenu);mbar.add(helpmenu);
            filemenu.add(newfile);filemenu.add(openfile);filemenu.add(savefile);filemenu.add(savefileas);
            helpmenu.add(about);
            tabbedpane.addTab("Untitled",scrollpane);
            
            
            //Finalize Layout
            setTitle("DK Programming Editor");
            setSize(900,900);
            
            //What has to happen when you click the cross in the right corner
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        
        void addMenuListener(ActionListener mal){
            about.addActionListener(mal);
        }
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class EditorController {
        
        private EditorView e_view;
        private EditorModel e_model;
        
        /* Constructor */
        public EditorController(EditorView view, EditorModel model){
            // The controller needs to interact with the view and the model
            e_view = view;
            e_model = model;
            
            // Add Listeners
            view.addMenuListener(new MenuListener());
        }
        
        // inner class MenuListener used to control the JMenuItems
        class MenuListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, "DK Programming Editor");
            }
        }//End inner class MenuListener
    }
    Java Code:
    public class EditorModel {
    
    }
    The Model is not used until now, but my questions is: Is it possible to add more JMenuItems to MenuListener.

    I tried to do this:

    public void actionPerformed(ActionEvent e){
    if(e.getSource() == about){
    JOptionPane.showMessageDialog(null, "DK Programming Editor");
    }
    }

    But that doesn't work. How can i solve that?

    keffie91
    Never give up! ;)

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    try this one
    Java Code:
    public void actionPerformed(ActionEvent e){
       if(e.getActionCommand().equals("About")){
             JOptionPane.showMessageDialog(null, "DK Programming Editor");
       }
    }

  3. #3
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. ur help on my question
    By jameela in forum New To Java
    Replies: 4
    Last Post: 10-13-2008, 12:38 PM
  3. JSP Question
    By maheshkumarjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 10:51 AM
  4. a question
    By slytheman in forum Java Servlet
    Replies: 0
    Last Post: 03-12-2008, 04:11 AM
  5. Need help on this question
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-27-2008, 03:58 PM

Posting Permissions

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