-
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.
-
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
}
}
Similar Threads
-
By Osiris43 in forum .NET
Replies: 1
Last Post: 08-04-2006, 12:15 PM
-
Replies: 5
Last Post: 01-15-2006, 07:10 PM
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
By Shailesh C.Rathod in forum .NET
Replies: 2
Last Post: 03-13-2002, 07:53 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks