如何将 Java Deque转换为 Java Deque进入默认列表模型?
我编写了一个类(我们称之为 Model.java),其中包含一个 Deque
,以及用于将项目入队和出队的方法。现在我试图将其与 GUI JList 联系起来。我对如何以某种方式使用我的“模型”数据(Deque)作为 JList 想要的 DefaultListModel 感到困惑。我仍在努力真正理解面向对象的概念,因为它们适用于 GUI 编程。 DefaultListModel 文档指出:
此类松散地实现了 java.util.Vector API,因为它实现了 java.util.Vector 的 1.1.x 版本,没有集合类支持,并且在发生更改时通知 ListDataListener。目前它委托给一个 Vector ....
有什么方法可以让 DefaultListModel 使用我的 Deque
而不是 Vector,从而允许我的 Model.java 代码在提供时保持基本不变所有的监听/通知行为都是免费的吗?或者我是否必须重写 Model.java 以使用 DefaultListModel 而不是 Deque
?
I wrote a class (let's call it Model.java) that contains a Deque<T>
, with methods for enqueuing and dequeuing items. Now I'm trying to tie this to a GUI JList. I'm baffled by how to somehow use my "model" data -- the Deque -- as a DefaultListModel that JList wants. I'm still struggling to really get OO concepts, as they apply to GUI programming. DefaultListModel documentation states:
This class loosely implements the java.util.Vector API, in that it implements the 1.1.x version of java.util.Vector, has no collection class support, and notifies the ListDataListeners when changes occur. Presently it delegates to a Vector ....
Is there some way to get the DefaultListModel to use my Deque<T>
instead of a Vector, thus allowing my Model.java code to remain largely unchanged while providing all the listening/notifying behavior for free? Or do I have to rewrite Model.java to use a DefaultListModel instead of Deque<T>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,
JList
构造函数采用ListModel
(接口),而不是DefaultListModel
(实现)。这是一个 OO 原则(契约),指定JList
可以使用恰好实现ListModel
接口的任何对象。来自面向对象编程概念的 Java 教程:由于
ListModel
只有四个方法,因此您的类应该很容易实现它们并将操作委托给内部Deque
。您的类应声明为并将包含四个实现
ListModel
方法的附加方法。这些实现可以在幕后执行您需要的任何操作,但必须遵守ListModel
的定义以及 JavaDoc 中作为ListModel
契约的一部分指定的任何行为。完成此操作后,您可以构造一个
JList
,将类Model
的实例传递给构造函数。Notice that the
JList
constructor takes aListModel
(an interface), not aDefaultListModel
(an implementation). This is an OO principle (Contract) specifying thatJList
can use ANY object that happens to implement theListModel
interface. From the Java tutorial on Object Oriented Programming Concepts:Since
ListModel
has only four methods, it should be very easy for your class to implement them and delegate the operations to your internalDeque
. Your class should be declared asand will contain four additional methods that implement the methods of
ListModel
. The implementations can do whatever you need under the covers, but must adhere to the definition ofListModel
and whatever behavior is specified as part of theListModel
contract, in the JavaDoc.Once you have done this, you can construct a
JList
passing an instance of your classModel
to the constructor.对于
JList
,您不必必须使用DefaultListModel
,只需使用ListModel
接口的一些实现即可。使用Deque
可以很容易地实现后者。For
JList
, you don't have to useDefaultListModel
, just some implementation of theListModel
interface. And the latter is very achievable using aDeque
.AbstractListModel
可能是一个很好的起点,因为它已经实现了规定的EventListenerList
方法处理侦听器和事件。AbstractListModel
might be a good starting point, as it already implements the prescribedEventListenerList
methods to deal with listeners and events.