简体   繁体   English

JList和ListModel设计模式建议

[英]JList and ListModel design pattern advice

I am building an application which is basically just a GUI for displaying and editing one single object. 我正在构建一个应用程序,它基本上只是一个用于显示和编辑单个对象的GUI。 Since the class for this object was created with JAXB , it is basically a tree with many Strings and integers as children, but there are also some List<E> s. 由于此对象的类是使用JAXB创建的,因此它基本上是一个具有许多字符串和整数作为子项的树,但也有一些List<E> For each simple child JAXB provides a getter and a setter, but for all lists only a getter is provided, since it gives a reference to the internal list, which can now be modified. 对于每个简单的子代,JAXB提供了一个getter和一个setter,但是对于所有列表,只提供了一个getter,因为它提供了对内部列表的引用,现在可以对其进行修改。

Since I need to display every child (and branch for that matter) in a separate swing GUI component, I need these views to handle some data. 因为我需要在一个单独的 swing GUI组件中显示每个子节点(以及那个分支),所以我需要这些视图来处理一些数据。 According to my current knowledge about the model view controller design pattern, I should strictly separate the model from the view. 根据我目前对模型视图控制器设计模式的了解,我应该严格地将模型与视图分开。 Following this, it would be a bad idea to let a JList operate directly on an internal list of my base object. 在此之后,让JList直接在我的基础对象的内部列表上操作是一个坏主意。 (Actually doing so would be a pretty easy solution to my specific use case, but since additional functionality might be added later on, I think I should refrain from this idea.) (实际上这样做对我的特定用例来说是一个非常简单的解决方案,但由于以后可能会添加其他功能,我想我应该避免这个想法。)

Then I started thinking: If I do not let the components work on the list directly, I have to make them use a copy of the original. 然后我开始思考:如果我不让组件直接在列表上工作,我必须让它们使用原始的副本。 But since I cannot set the original list to a new one returned by my GUI component, I have to copy the items another time when saving the view data to my model. 但由于我无法将原始列表设置为GUI组件返回的新列表,因此在将视图数据保存到模型时,我必须再次复制项目。 Also, it would not be wise to hand out references to internal lists of a custom ListModel , which would mean, that depending on the depth of the GUI structure, the list may be copied additional times. 此外, ListModel对自定义ListModel内部列表的引用是不明智的,这意味着,根据GUI结构的深度,列表可能会被复制多次。

Since I also want my application to be efficient, this does also not seem like the correct approach. 由于我也希望我的应用程序高效,这似乎也不是正确的方法。 How do I build this "correctly"? 如何“正确”构建这个?

Summary: 摘要:

  • The List<E> from the original object is a reference to an internal list of the object. 原始对象中的List<E>是对对象内部列表的引用。

  • The JList displaying the list should not get this reference, hence it must copy the list. 显示列表的JList不应该获得此引用,因此它必须复制列表。

  • Getting the list from the JList should not yield in a reference to the internal list either, hence it must be copied again. JList获取列表也不应该在对内部列表的引用中产生,因此必须再次复制它。

  • Saving the list to the original object must copy the list a third time, because the original object does not have a setter method for the list. 将列表保存到原始对象必须第三次复制列表,因为原始对象没有列表的setter方法。 (see above for details) (详见上文)

Is my grasp on this correct? 我掌握这个是正确的吗? How do I build this properly and make it efficient? 如何正确构建并使其高效?

PS: Adding setter methods to the original class structure is not an option! PS:将setter方法添加到原始类结构不是一个选项!

was created with JAXB , it is basically a tree with many Strings and integers as children, but there are also some List<E> s. 是用JAXB创建的,它基本上是一个有许多字符串和整数作为子节点的树,但也有一些List<E> For each simple child JAXB provides a getter and a setter, but for all lists only a getter is provided, since it gives a reference to the internal list, which can now be modified. 对于每个简单的子代, JAXB提供了一个getter和一个setter,但是对于所有列表,只提供了一个getter,因为它提供了对内部列表的引用,现在可以对其进行修改。

see 看到

All of the components suggested by @mKorbel provide some kind of selection listener that will allow you to loosely couple a selection in one panel to the selected item's display in another. @mKorbel 建议的所有组件都提供了某种选择监听器,允许您将一个面板中的选择松散地耦合到另一个面板中的所选项目的显示。 FileBrowser illustrates a TreeSelectionListener ; FileBrowser演示了一个TreeSelectionListener ; Outline can have a ListSelectionListener , shown here ; Outline可以有一个ListSelectionListener所示, 在这里 ; etc. 等等

Addendum: In this related example , DisplayPanel listens to an adjacent TableModel , which it can query to update a model in one of it's own components. 附录:在此相关示例中DisplayPanel侦听相邻的TableModel ,它可以查询以更新其自己的组件中的模型。 Note that each model is loosely coupled to its respective view. 请注意,每个模型都松散地耦合到其各自的视图。 Common notification mechanisms are mentioned here . 这里提到通用的通知机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM