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:
Code :
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:
Code :
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):
Code :
public void modifyProfile(String file) throws IOException{
File open = new File(file + ".txt");
Desktop desktop = Desktop.getDesktop();
desktop.edit(open);
super.run();
}//end modifyProfile
Re: Model View Controller help
Re: Model View Controller help
Quote:
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.