tags:

views:

90

answers:

4

I'm looking for some guidance on best practices for an application using Java Swing to be structured. I'm a webapp guy normally and try to follow MVC. Is MVC typical for Swing apps? If so, how?

+1  A: 

Following MVC is a very good idea. As far as I know there is no formal Java Swing framework doing that, they all concentrate on "View" aspect of the problem.

As far as app structure and MVC - the best framework I know is Griffon. It is not Java - it is Groovy, but that is what makes it more attractive and pretty easy to learn. Griffon to a Swing app is the same as Grails to a web app.

Check it out at http://griffon.codehaus.org/

eugener
A: 

Swing has a relatively good setup for an MVC architecture, but really it combines the view and controller. Components in Swing can have listeners attached to them, which is the controller aspect, and then from within these listeners (which are within the components) you can modify the view and model accordingly. So to answer your question, yes, Swing would help with an MVC approach to a Java application.

In Swing, you still have two separate pieces for VC: components and listeners. The reason I say they're combined is because each component can have its own respective listener. These listeners typically do the manipulation, and are the controller aspect. The components are designed to not only be viewed on the screen, but also pass information to these listeners. Button presses, key strokes, focus changes, window closings, etc. are all reported to the listeners of the respective component by the component. Here's a simple code snippet for a button with an action listener, which registers a button press.

ActionListener buttonListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        // Controller code
    }
};

JButton button = new JButton("A Button");
button.addActionListener(buttonListener);

The ActionListener is essentially the controller, but it depends on button to receive input from the user. This is where Swing muddles the difference between view and controller from a pure MVC standpoint, but at the same time, the distinction is still there:

Listeners are the controllers (minus directly interpreting the input)
Components are the view (but interpret input and pass it to the controller)

Hope that helps :)

peppertherj
I don't think listeners ARE controllers. They simply can be used in a controller to respond to component events.
eugener
That's what I meant. In terms of MVC, they aren't, but they can be thought of in that way. Controllers take the input and modify the model. Listeners may not necessarily take the input, but they can certainly be used to manipulate the model and, as is with some interpretations of MVC, modify the view. It all depends on which part of the controller's responsibilities you wish to put more weight.
peppertherj
A: 

You may find this very simple example and discussion helpful.

trashgod
A: 

There was a JSR (296) which describes a framework for building Swing GUI's, but it looks like it's been forgotten about, check out the following article though:

http://java.dzone.com/news/jsr-296-end-jframe

I did find the following frameworks, initially based off it, which look promising:

Would be interested in finding out how these work out in practice. Hope they help.

Jon
There's also this (but it looks dormant) - http://github.com/hamnis/SAFF
Jon