更新 Java Swing 应用程序中的按钮状态
我已经实现了一个基于播放列表概念的基本 Swing GUI,作为更大的 DJ 音频混合应用程序的一部分(想想原始的 Virtual DJ(!))。播放列表基本上由一个 JTable 组成,可以将曲目从 JTree 拖放到该 JTable 上。包含该表的 JPanel 还包含一组用于保存、加载和删除播放列表以及加载播放器的按钮。这些按钮状态需要根据应用程序的当前状态进行更新(启用/禁用)(即是否加载了播放列表?播放列表中是否有曲目?等)
我试图遵循 MVC 模式,其中底层 TableModel通过控制器与表格和按钮 UI 分开。
我的问题是......处理按钮更新的最优雅/最有效的方法是什么?此时,对当前加载的播放列表文件的引用存储在控制器中,并且控制器确定模型中的播放列表是否为空。然后,它将两个布尔值(isEmpty、isLoaded)发送到视图,然后视图根据这些值确定要启用哪些按钮。这个方法看起来有点不优雅,所以我欢迎任何建议。
谢谢!
I've implemented a basic Swing GUI based on the concept of a playlist as part of a larger DJ audio mixing application (think a primitive Virtual DJ(!)). The playlist basically consists of a JTable onto which tracks can be tracks can be dragged n' dropped from a JTree. The JPanel that incorporates the table also contains a suite of buttons for saving, loading, and deleting playlists as well as for loading the player(s). These button states need to be updated (enabled/disabled) based on the current state of the application (i.e. is a playlist loaded? Are there tracks in the playlist? etc.)
I'm trying to follow an MVC pattern whereby the underlying TableModel is separated from the table and button UI through a controller.
My question is...What is the most elegant/efficient way to handle the button updates? At the moment, a reference to the currently loaded playlist file is stored in the controller, and the controller determines whether a playlist is empty from the model. It then sends two boolean values (isEmpty, isLoaded) to the View, which then determines which buttons to enable based on these values. This method seems a little inelegant, so I'd welcome any suggestions.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解 MVC,理论上:
实践中的问题是您的域模型(您的类)与 Swing 模型类不同。所以问题是谁在同步域和 Swing 模型:控制器还是视图?
控制器同步它们。当域数据更改时(播放列表是否为空)控制器将更改推送到视图。在这种情况下,Controller 更加复杂,并且依赖于域模型和 Swing 模型。此外,当您拥有一个域模型和多个视图(显示相同底层数据的树和表)时,该模型也会变得复杂。
视图适应域模型。当领域模型数据发生变化时,控制器只是向视图指示它应该自行更新。在这种情况下,View 依赖于域类,但 Controller 不依赖于 Swing Model 或 View,只是它请求更新。
据我所知,您实现了选项 1。
我个人会使用选项 2。其中控制器处理业务逻辑和域模型,视图处理同步到域模型并构建视图层次结构。
As I understand MVC, theoretically:
The problem in practice is that your domain model (your classes) is not the same as Swing Model classes. So the question is who is syncing domain and swing models: Controller or View?
Controller syncs them. When domain data changes (playlist is empty or not) Controller pushes changes to View. In this case Controller is more complex and is dependent on both domain model and Swing Model. Also this model becomes complex when you have one domain model and multiple views (Tree and Table showing same underlying data).
View adapts to domain model. When domain model data changes, Controller simply indicates to View that it should update itself. In this case View is dependent on domain classes, but Controller is not dependent on Swing Model or View, except that it requests an update.
As I see you implemented option 1.
Personally I'd use option 2., where Controllers deal with business logic and domain model and View deals with syncing to domain model and building up view hierarchy.
您可以使用侦听器和操作更优雅地完成此操作。为每个按钮使用 AbstractAction。 (您也可以将它们重复用于菜单项。)然后您可以启用/禁用该操作,按钮将更新以匹配。如果您将 TableModelListener 添加到表模型中,它将收到所有更改的通知,并可以对操作进行更新。
您不会创建自己的 MVC。您将在 Swing 类本身中使用固有的 MVC。
You can do this more elegantly using listeners and Actions. Use an AbstractAction for each button. (You can reuse them for menu items as well.) Then you can enable/disable the action and the button will update to match. If you add a TableModelListener to your table model it will be notified of all changes and can make the updates to the actions.
You won't be creating your own MVC. You will be using the inherent MVC in the Swing classes themselves.