Controller Class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Controller Class

  1. #1
    Join Date
    May 2006
    Posts
    12

    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.

  2. #2
    Join Date
    Dec 2006
    Location
    Kerala,India
    Posts
    51
    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

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 01:15 PM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  3. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 04:00 PM
  4. Replies: 5
    Last Post: 10-17-2002, 02:58 PM
  5. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 08: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center