Swing的JList MVC实现有什么问题吗? [英] Is there something wrong with Swing's MVC implementation for JList?

查看:87
本文介绍了Swing的JList MVC实现有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我问过这个问题。所有解决方案都是解决方法。

Some time ago I asked this question. All solutions are workarounds.

现在这不可能。我觉得这里出了点问题,但是我不知道是Swing的MVC模型在概念上是错误的,还是我的想法在概念上是错误的。

Now this can't be. I feel that something is wrong here, but I can't tell if it is Swing's MVC model that is conceptually wrong, or if it is my thinking that is conceptually wrong.

这是问题了。我使用 JList 来实现文档页面的缩略图列表。如果用户从列表中选择另一个缩略图,则加载该页面。为此,我将 ListSelectionListener 添加到 JList ,当选择更改时,它会加载该页面。但用户也可以使用其他控件更改页面。当然,我希望通过在此处选择该页面来将其反映在缩略图列表中。所以我 setSelectedIndex()更新 JList 。不幸的是,这会产生不必要的效果,即引发 ListSelectionEvent ,这会导致侦听器重新加载页面。

Here is the problem again. I am using a JList to implement a list of thumbnails for the pages of a document. If the user selects another thumbnail from the list, that page is loaded. To do this I added a ListSelectionListener to the JList, which when the selection changes, it loads that page. But the user can also change the page using another control. Naturally, I want this to be reflected in the thumbnail list by having that page selected here. So I setSelectedIndex() to update the JList. Unfortunately this has the unwanted effect of raising a ListSelectionEvent which causes the listener to reload the page.

现在是什么错了吗?我只是从其他地方改变了模型,所以我自然希望视图自我更新,但我不希望它触发事件。 Swing没有实现MVC吗?或者我在这里错过了一点?

Now what is wrong here? I just changed the model from somewhere else, so naturally I want the view to update itself, but I don't want it to trigger events. Is Swing not implementing MVC right? Or am I missing a point here?

推荐答案

这是我们很多Swing程序员必须面对的问题:多个控件修改相同的数据,然后在每个控件中反映更新。在某些时候,必须对将要应用于模型的更新进行最终否决:无论什么东西需要能够处理多个(可能是多余的甚至是相互矛盾的)更新并决定如何处理它们。这可能发生在模型层中,但理想情况下它应该是执行此操作的控制器 - 毕竟,这件事很可能是业务逻辑所在的位置。

This is an issue that a lot of us Swing programmers have to face: multiple controls modifying the same data, with the update then reflected in each control. At some point, something has to have the ultimate veto on what updates will be applied to the model: whatever that something is needs to be able to handle multiple (potentially redundant or even contradictory) updates and decide what to do with them. This could happen in the model layer, but ideally it should be the controller that does this - this piece, after all, is most likely where the business logic resides.

在这方面,Swing的问题在于MVC的控制器部分经常在视图组件和模型之间分开,因此很难将该逻辑集中。在某种程度上, Action 接口通过将逻辑放在一个位置并允许它由不同组件共享来纠正actionPerformed()事件,但这对于其他类型的事件,或者当需要协调多个不同类别的事件时。

The problem with Swing, in this regard, is that the controller piece of MVC is often split somewhat between the view component and the model so it can be difficult to have that logic centralised. To some extent the Action interface rectifies this for actionPerformed() events by putting the logic in one place and allowing it to be shared by different components, but this doesn't help for the other types of event, or when there are multiple different classes of event that need to be coordinated.

然后,答案是遵循一个暗示的模式。 Swing但未明确:仅在状态实际发生变化时执行请求的更新,否则不执行任何操作。一个例子是在 JList 本身:如果你试图将 JList 的选定索引设置为相同的已经选择的索引什么都不会发生。不会触发任何事件,也不会发生更新:有效地忽略更新请求。这是件好事。这意味着您可以在 JList 上拥有一个侦听器,它将响应新选择的项目,然后再询问相同的 JList 重新选择相同的项目,你不会陷入病态递归循环。如果应用程序中的所有模型控制器执行此操作,那么在整个地方触发多个重复事件就没有问题 - 每个组件只会在需要时更新自身(并随后触发事件),如果它确实更新然后它可以触发它想要的所有更新事件,但只有那些尚未收到消息的组件才会对它做任何事情。

The answer, then, is to follow a pattern that's hinted at in Swing but not made explicit: only perform the requested update if the state will actually change, otherwise do nothing. An example of this is in the JList itself: if you attempt to set the selected index of a JList to the same index that's already selected nothing will happen. No events will be fired, no updates will occur: the update request is effectively ignored. This is a good thing. This means that you can, for example, have a listener on your JList that will respond to a newly selected item, and then in turn ask the same JList to reselect that same item and you won't get stuck in a pathologically recursive loop. If all the model-controllers in an application do this then there's no problem with multiple, repeated events firing off all over the place - each component will only update itself (and subsequently fire off events) if it needs to, and if it does update then it can fire off all the update events it wants, but only those components that haven't already got the message will do anything about it.

这篇关于Swing的JList MVC实现有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆