Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Model View Controller help

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Model View Controller help

    I'm working on the interface for my project but I'm having some trouble (code below). I'm trying to test 'modify' for now, which is supposed to allow the user to enter the name of the file they want to modify and then it is opened up on notepad. The problem is when I press the modify button it automatically gives me an error that the file does not exist, but I haven't entered anything into the text field yet. How do I change it so I can enter text, before it jumps into trying to open the file? :s

    Controller class:
    import java.awt.event.*;
    import java.io.IOException;
     
    public class Controller{
     
    	private Model m_model;
    	private View m_view;
     
    	Controller(Model model, View view) {
     
    		m_model = model;
    		m_view = view;
     
    		view.addModifyListener(new ModifyListener());
    	}//end constructor
     
    	private class ModifyListener implements ActionListener {
     
    		public void actionPerformed(ActionEvent e){
     
    			m_view.modifyView();
    			String input = "";
    			try {
    				input = m_view.getInput();
    				m_model.profileModify(input);
    			} catch (IOException e1) {
    				System.out.println("Error: " + e1);
    			}
    		}
    	}

    View class:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
     
    import java.awt.event.*;
     
    public class View extends JFrame{
     
    	private static final String INIT_VALUE = "1";
     
    	private JButton createBtn = new JButton("Create");
    	private JButton modifyBtn = new JButton("Modify");
    	private JButton deleteBtn = new JButton("Delete");
    	private JButton checkVulBtn = new JButton("Check Vulnerabilities");
    	private JButton searchVulBtn = new JButton("Search Vulnerabilities");
    	private JTextField choiceTxt = new JTextField(10);
    	private JTextField fileNameTxt = new JTextField(10);
     
    	private Model mod;
     
     
    	public View(Model model){ //constructor
     
    		mod = model;
     
    		setBounds(100, 100, 450, 300);
    		JPanel content = new JPanel();
    		content.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(content);
    		content.setLayout(new FlowLayout());
    		createBtn.setBounds(98, 66, 264, 29);
    		modifyBtn.setBounds(98, 66, 264, 29);
    		deleteBtn.setBounds(98, 66, 264, 29);
    		checkVulBtn.setBounds(98, 107, 264, 29);
    		searchVulBtn.setBounds(98, 148, 264, 29);
     
    		content.add(new JLabel("What would you like to do?"));
    		content.add(createBtn);
    		content.add(modifyBtn);
    		content.add(deleteBtn);
    		content.add(checkVulBtn);
    		content.add(searchVulBtn);
     
    		this.setContentPane(content);
    		this.pack();
    		this.setTitle("Vulnerability Assessment Tool");
     
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		}
     
    	public void modifyView(){
     
    		setBounds(100, 100, 450, 300);
    		JPanel content = new JPanel();
    		content.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(content);
    		content.setLayout(new FlowLayout());
    		modifyBtn.setBounds(98, 66, 264, 29);
     
    		content.add(new JLabel("Enter the name of the profile you would like to modify: "));
     
    		content.add(fileNameTxt);
    	}
     
    	public String getInput(){
     
    		return fileNameTxt.getText();
    	}
     
    	public void addModifyListener(ActionListener modl){
    		modifyBtn.addActionListener(modl);
    	}
    }

    Modify method (called by Model class):
    public void modifyProfile(String file) throws IOException{
     
    		File open = new File(file + ".txt");
    		Desktop desktop = Desktop.getDesktop();
    		desktop.edit(open); 
    		super.run();
     
    	}//end modifyProfile


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Model View Controller help

    What's the question?

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Model View Controller help

    Modify method (called by Model class):
    You didn't include the Modify class. How are we to determine where the problem is? Or did you want to get the file name from the user inside the method? (1 not a great idea and 2 it looks like you plan to pass the filename as a parameter but you didn't include the code that calls the method.

Similar Threads

  1. Web mvc controller - Brutos
    By urubu in forum Web Frameworks
    Replies: 6
    Last Post: July 28th, 2013, 12:09 PM
  2. Controller for date class
    By Ecosse in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2012, 10:21 AM
  3. Replies: 1
    Last Post: September 18th, 2011, 10:06 AM
  4. Killing view of a dead model
    By Alice in forum Java Theory & Questions
    Replies: 9
    Last Post: May 30th, 2011, 05:27 AM
  5. creating a controller to allow instances to be created from keyboard
    By ss7 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 2nd, 2009, 01:30 PM