Controller Class
Hi!
I am creating a simple app that will be able to open up a text file/url.
The path will be input via a gui.
There is also the ability to search for text in the file via the gui.
Using the idea of MVC, I created 3 classes:
1) gui class
2) model class
3) Controller Class
I am having trouble implementing the Controller class as follows:
When the search button from the gui class is clicked, the Controller Class will handle the generated event.
Within the actionPerformed method the controller class first figures out which JButton was clicked.
Then, let's say the search button was clicked, takes the search String from the GUI and executes the search via method search(String str) in the model class.
How can I access my model (and view) objects from within the Controller class?
Note- I'd prefer not to have any inner classes.
[918 byte] By [
pacify] at [2007-11-11 9:59:15]

# 1 Re: Controller Class
Yes you can do that.
See the following code. In this Controller update the View in a loosly coupled manner.
interface View {
public void update(String str);
}
class MyView implements View{
public void update(String str){
//logic for updating
}
public static void main(String args[]){
View mv = new MyView();
Button b = new Button("Test");
b.addActionListener(new ActionController(mv));
}
}
class ActionController implements ActionListener {
View v = null;
Model m = null;
public Controller(View view){
this.v = view;
m = new Model();
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Test")){
String str = m.serach(e.getActionCommand());
//update the view
v.update(str);
}
}
}
class Model {
public Model(){
}
public String search(String str){
//implementation of serach
}
}