开发者

Using the MVC model with Swing Application Framework

开发者 https://www.devze.com 2022-12-19 16:10 出处:网络
I\'m trying to create a simple Java desktop application using the Swing Application Framework and the MVC model but I\'m struggling on some areas because there is a lack of good examples (the only SAF

I'm trying to create a simple Java desktop application using the Swing Application Framework and the MVC model but I'm struggling on some areas because there is a lack of good examples (the only SAF examples I have found are anything but MVC!).

I manage to fire events from components, but apart from that I struggle to use the MVC model with SAF. Are there any examples anywhere?

开发者_开发百科

For instance, I fire an event (mapped with @Action) in the Viewer which sends it to the controller. But which function should I use? My AbstractController extends PropertyChangeListener.

How do I do the binding with SAF to both directions (model -> controller and view -> controller)?


I can recommend this article: A Swing Architecture Overview.

In example, when using JTable (view), you create a model by extending AbstractTableModel, and you handle user-events by Actions and listeners.

A user could write some text in a JTextField, and you bind an Action to an "Add"-JButton. Your Action implement actionPerformed() where you can call an Add-method in the model to add the text. In the Add-method you save the data and then calls fireTableRowsInserted(), and the view will be updated.

Swing components contains often a model and a view by default.

EDIT: Sorry, didn't know about Swing Application Framework. My answer was just addressing Swing.

0

精彩评论

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