模型可以刷新视图吗?
我有一个使用 SWT 的 MVCish 设计。我有一个实现整个 GUI(按钮和操作侦听器)的类,它有一个 Shop 对象,其中有一个项目列表。有时,使用 RMI(学校作业),列表会被更新(如果杂志中有新内容,它会告诉服务器,服务器会将新列表传播到所有商店),这一切都很酷。问题是我不知道如何让 GUI 无需按下按钮即可重新绘制新列表。我想做的:
1)杂志里有新东西
2)它将新列表发送到服务器
3)服务器将列表发送给所有商店
4)每个商店更新其列表
5) 每个商店以某种方式告诉 GUI 重新绘制代表列表的 JTree。
我已经完成了第 1-4 点。 5)是否可以实施?我故意尝试将模型和控制器分开(因为我们必须制作终端和 GUI 界面),但现在我看到商店获得了新列表,并且它甚至不知道 GUI 的存在(只是就像普通的 MVC)。
现在我有一个“刷新”按钮,它从 Shop 对象中获取列表并更新 JTree 模型,但根据我的老师告诉我的内容,他希望它是自动的。我可以通过在一个类中编写商店和 GUI 来做到这一点,但这有点糟糕。
I have a MVCish deisgn using SWT. I have a class which implements the whole GUI (buttons and action listeners) and it has a Shop object, which has a list of items. From time to time, using RMI (school assignment), the list is being updated (if there is something new in the magazine it tells that to the server and the server propagates the new list to all shops) and it's all cool. The problem is I have no idea how to make the GUI repaint the new list withough pressing a button. What I want to do:
1) there's new stuff in the magazine
2) it sends the new list to the server
3) the server sends the list to all shops
4) each shop updates its list
5) each shop somehow tells the GUI to repaint the JTree representing the list.
I've already done points 1-4. Is it possible to implement 5)? I intentionally tried to separate the model and the controller (since we have to make a terminal and a GUI interface) but now I see that the shop gets the new list and that it doesn't even know about the existance of the GUI (just like a normal MVC).
Now I have a "Refresh" button which takes the list from the Shop object and updates the JTree model, but from what my teacher told me he wants it to be automatic. I could do it by writing both the shop and the gui in a single class, but that kinda sucks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使视图成为模型的观察者,并以这种方式获取通知。或者,您可以安排定期任务来执行刷新操作。
从用户界面的角度来看,我不希望视图自动更新——如果我正在深入查看某些内容,突然树被重置,我会很恼火。我认为显示诸如“可用目录更新,按‘刷新’查看新项目”之类的消息(有点像“新答案”消息)或者只是突出显示刷新按钮(更改其背景颜色或其他内容)要友好得多)。
You can make your view an Observer of the model, and get notifications that way. Or else you can schedule a periodic task to do your refresh action.
From a UI point of view, I wouldn't like the view being automatically updated -- if I was drilling down to something and suddenly the tree was reset, I'd be annoyed. I think it's far friendlier to display a message like "Catalog updates available, press 'refresh' to see new items" (kind of like the SO "new answers" message) or maybe just highlight the refresh button (changing its background color or something).
您正在寻找的称为数据绑定。数据绑定是将模型和视图连接在一起的想法,以便更新的模型自动更新视图。
不幸的是,通过快速搜索,似乎没有可用于 JTree 的数据绑定解决方案。我可能是错的,但搜索让我相信你必须自己写一个。
一种简单的方法(尽管可能不是最好的解决方案)是在模型上引用 JTree。然后,您可以在模型以需要更改视图的方式更改时告诉 JTree 进行刷新。
What you are looking for is called Data Binding. Data Binding is the idea of connecting a model and a view together so that the model being updated automatically updates the view.
Unfortunately with a quick search, it doesn't look like there is a data binding solution available for JTree. I could be wrong, but a search leads me to believe you would have to write one yourself.
An easy way (although perhaps not the best solution) would be to have a reference to the JTree on the model. You can then tell the JTree to refresh any time the model is changed in a way that would require the view to change.