开发者

Java Swing Program Structure [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 8 years ago.

Improve this question 开发者_高级运维

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?


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/


You may find this very simple example and discussion helpful.


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 :)


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:

  • GUTS GUI - http://kenai.com/projects/guts/pages/Guts-gui
  • BSAF - http://kenai.com/projects/bsaf

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜