File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "MVC" Watch "MVC" New topic
Author

MVC

Fred Barnes
Ranch Hand

Joined: Jun 11, 2002
Posts: 62
Hi Guys,
I am trying to implement MVC in my GUI client.
If I have one class with all my Swing components for the view and one controller class implementing all my Listeners. How do I determine which component was select/clicked?
public class Controller implements ActionListener {
public void actionPerformed(ActionEvent ae) {
Object o = ae.getSource();
//???
}
}
public class GUI extends JFrame {
private Controller c;
private JButton button1;
private JButton button2;
private JPanel panel;
public GUI() {
c = new Controller();
button1 = new JButton("One");
button2 = new JButton("Two");
button1.addActionListener(c);
button1.addActionListener(c);
panel = new JPanel();
panel.add(button1);
panel.add(button2);
getContentPane().add(new SearchPanel());
pack();
setVisible(true);
}
}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

What I did is include hook methods in the Swing GUI class, so that my Controller can then hook into the Listeners and call it's method when that action occurs.
Now Eugene also has a great design in further decoupling by creating Action classes that hook into the GUI and also "Hook" into the Controller, so that the Action class is the only class that knows the GUI and Controller, and the GUI and Controller don't know anything about each other. His actually goes a little further, but he knows it better.
Here is an example of a Hook method in my GUI.

This will then allow me to call

in a method in my Controller.
Now the two above don't match but you can get the idea from there.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Bernhard Woditschka
Ranch Hand

Joined: Sep 16, 2002
Posts: 89
Hi guys,
I've been playing around with different configurations of MVC for the last few days.
I tested total seperation:
class Model holds the model of the reservetion dialog
class View configures the reservation panel, has methods to configure the view(enable buttons...) and inner classes that call the controller
class controller knows the view, the model and tne buisines layer
class TravelAgent implements the busines layer (lookup airports, flights book flight ant talks to the database
Last night i concluded that the design gets too complicated so I asked myself what this effort is good for? Code reuse?
I replaced Model, View and Controller with one class:
My conclusion is that the gui component book flight is specialised and code reuse is no issue. Tje Busines Layer is well encapsulated - so I started to encapsulate the GUI:
class Reservationpane is a encapsulated reusable component that only has to be given a TravelAgent and to be placed somewhere in the GUI to work.
It sets up the Gui, and iplements the controller with inner classes, that ocasionally call methods to make the implementation cleaner:


what do you think about this solution
Bern
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

OK Bern, here is the future enhancement question.
They no longer want to use your Swing screen, then want an HTML page instead how are they going to do that simply. In your design of one class, means that you have to completely recreate that class 100% with Controller Model and GUI to just change the GUI.
With MVC and seperation, you can swipe out the GUI without having to change any other class. So to speak.
In my case the instace of the GUI is just a different class and then I am finished with my changes.
Mark
Bernhard Woditschka
Ranch Hand

Joined: Sep 16, 2002
Posts: 89
Thats right, but the Controller part just forwards the user gestures to the busines logic and feeds the response to the model.
I think this can not essily be reused for HTML pages. The reuse of my design starts at the busines logic layer with methols like
getOriginAirports();
...
bookFlight(Strng flightNumber, int seats);
do you thing i am missing an important point?
Bern
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes. In the instructions it even mentions MVC for the GUI and if you don't follow it, they will mark you down.
There are good and many reasons for the MVC pattern to be created in the first place and used extensively in Swing to try to use something else. Using a standard like MVC allows for better maintenance since it is a structure that everyone can know, whereas your design is tougher to maintain and understand by other programmers.
If I can ever teach someone the most important part of programming; maintainability, readability, don't go beyond what is asked, and keep it as simple as possible, is what I'd teach over and over again.
Mark
Bernhard Woditschka
Ranch Hand

Joined: Sep 16, 2002
Posts: 89
I'm confused. It looks like the MVC pattern exists on multiple levles.
One level is MVC on component level with the View and controller with hooks (Events) e.g. JTable, JComboBox, etc. and the Model e.g. DefaultTableModel,...
I can imagine that ther could be a second level of MVC but i'm not quite sure about it.
Bern
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

It looks like the MVC pattern exists on multiple levles. One level is MVC on component level with the View and controller with hooks (Events) e.g. JTable, JComboBox, etc. and the Model e.g. DefaultTableModel,...

That's exactly right. Most Swing components implement their own MV pattern. But think of your application as a software component, and the need for an application-level MVC (or MV) becomes obvious. Your application model may end up using some Swing models (such as table model), and that's perfectly fine.
Eugene.
[ January 02, 2003: Message edited by: Eugene Kononov ]
Fred Barnes
Ranch Hand

Joined: Jun 11, 2002
Posts: 62
Hi Guys,
I still do not understand how you get to determine which component was clicked/selected?
Please help?
Regards
Fred
Fred Barnes
Ranch Hand

Joined: Jun 11, 2002
Posts: 62
Hi Guys,
Is this implementing MVC in my GUI?
public class Controller implements ActionListener {
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("ONE")) {
//Do one
}
if(ae.getActionCommand().equals("TWO")) {
//Do two
}
}
}
public class GUI {
private Controller controller;
private JButton button1;
private JButton button1;
public GUI() {
controller = new Controller();
button1 = new JButton("ONE");
button2 = new JButton("TWO");
button1.setActionCommand("ONE");
button2.setActionCommand("TWO");
button1.addActionListener(controller);
button2.addActionListener(controller);
}
}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Fred, what you did was to create a class that is an ActionListener, so this class can only really be used by one object on the GUI, unless you include a huge IF statements, which isn't very clean.
If you look at my example you can see wha tthe hook method does.
In your GUI you have public methods that allows any class to pass an instance of a Listener. In your Controller, you can create anonymous ActionListeners that it passes to the GUI, in the anonymous class inside the Controller you call a method in the Controller but outside of the anonymous class. This is how the controller knows what has but "clicked".
So in you GUI you have a hook method for each possible action you want to handle.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC