View Single Post
  #1 (permalink)  
Old 10-28-2008, 11:02 AM
keffie91's Avatar
keffie91 keffie91 is offline
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
Rep Power: 0
keffie91 is on a distinguished road
Default [SOLVED] MVC question
Hello I am rewriting my Programming editor by the MVC Model

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);
    }

}
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);
    }
}
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
}
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!
Reply With Quote