在多个视图中观察到的相同列表

发布于 2024-12-10 14:44:53 字数 1830 浏览 0 评论 0原文

我有两个使用相同域对象的视图(JPanel)。我的域对象包含一个 ObservableList。

ObservableList 是一个 LinkedList

private ObservableList<MyObject> listMyObject = ObservableCollections.
    observableList(Collections.synchronizedList(new LinkedList<MyObject>()));

在我的两个视图中,每次将一个元素添加到列表中时,我都会进行一些计算

protected class MyListDataListener implements ObservableListListener {
   public void listElementsAdded(ObservableList list, int index, int length) {
   MyObject obj = (MyObject)list.get(index);
   Poin2D location = obj.getObjLocation();
   location.setLocation(location.x + (time / getWidth()), location.y);
   obj.setObjLocation(location);
}

我遇到的问题是,由于每次将一个元素添加到列表中时,两个视图都使用相同的列表,因此位置会更新两次视图中移动的对象完成其动画的速度要快两倍。我希望每个添加的元素只更新一次。

public class MyFrame extends JFrame {
public MyFrame() {
View view1 = new View(domainObject.getMyDataList());
View view2 = new View(domainObject.getMyDataList());
}
}

public class View extends JPanel {
private ObservableList<MyObject> listMyObject;
private ObservableList<MyObject> otherList = ObservableCollections.
    observableList(Collections.synchronizedList(new LinkedList<MyObject>()));

public View(ObservableList<MyObject> listMyObject) {
this.listMyObject = listMyObject;
listMyObject.addListListener(new MyListDataListener());
}

protected class MyListDataListener implements ObservableListListener {
   public void listElementsAdded(ObservableList list, int index, int length) {
otherList.add((MyObject)list.get(index));
for(MyObject obj : otherList) {
Poin2D location = obj.getObjLocation();
   location.setLocation(location.x + (time / getWidth()), location.y);
   obj.setObjLocation(location);
}
}

如果我不创建 view2,一切正常。每次添加元素时都会创建 view2,每个视图都会迭代列表并更改对象的位置两次而不是一次。 谢谢你的帮助。

I have two views (JPanel) that uses the same domain object. My domain object contains a ObservableList.

The ObservableList is a LinkedList

private ObservableList<MyObject> listMyObject = ObservableCollections.
    observableList(Collections.synchronizedList(new LinkedList<MyObject>()));

In my two views I to do some calculation each time an element is added to the list

protected class MyListDataListener implements ObservableListListener {
   public void listElementsAdded(ObservableList list, int index, int length) {
   MyObject obj = (MyObject)list.get(index);
   Poin2D location = obj.getObjLocation();
   location.setLocation(location.x + (time / getWidth()), location.y);
   obj.setObjLocation(location);
}

The problem I have is that as both views use the same list each time one element is added to the list the location is updated two times the object that is moving in the view finish its animation two times faster. I would like it to be updated only one time for each element added.

public class MyFrame extends JFrame {
public MyFrame() {
View view1 = new View(domainObject.getMyDataList());
View view2 = new View(domainObject.getMyDataList());
}
}

public class View extends JPanel {
private ObservableList<MyObject> listMyObject;
private ObservableList<MyObject> otherList = ObservableCollections.
    observableList(Collections.synchronizedList(new LinkedList<MyObject>()));

public View(ObservableList<MyObject> listMyObject) {
this.listMyObject = listMyObject;
listMyObject.addListListener(new MyListDataListener());
}

protected class MyListDataListener implements ObservableListListener {
   public void listElementsAdded(ObservableList list, int index, int length) {
otherList.add((MyObject)list.get(index));
for(MyObject obj : otherList) {
Poin2D location = obj.getObjLocation();
   location.setLocation(location.x + (time / getWidth()), location.y);
   obj.setObjLocation(location);
}
}

If i don't create view2, everything is working fine. With view2 created each time an element is added each view iteratate the list and change the location of my object two times instead of one time.
Thank you for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光沙漏 2024-12-17 14:44:53

实际上,我不明白您的期望:

  • 您有一个主列表(myListData),
  • 该主列表上有两个侦听器,
  • 您有该列表的两个副本(otherList),其中包含与master
  • 在接收添加时,两个侦听器中的每一个都会操作副本中的元素:这些元素与 master 中的实例相同,因此它们操作了两次。

要解决此问题,请执行以下操作 :元素一旦在view, fi 通过将侦听器保持在外面:

// frame
getDataList().addListener(....);
new View(getDataList());
// view
... do nothing

Actually, I don't understand what you expect:

  • you have a master list (myListData)
  • you have two listeners on that master list
  • you have two copies of that list (otherList) containing the same instances as the master
  • on receving an add, each of the two listeners manipulates the elements in the the copy: those elements are the same instances as in the master, so they manipulated twice ..

To solve, do the manipulation of the elements once outside of the view, f.i. by keeping the listener outside:

// frame
getDataList().addListener(....);
new View(getDataList());
// view
... do nothing
零度℉ 2024-12-17 14:44:53

我认为您可以使用一些内置方法 (size()) 直接从 ObservableList 获取列表的大小,而不必保留单独的“count”变量。 (取决于这个 ObservableList 类的实现。)

I would think that you could get the size of the list from the ObservableList directly, using some built in method (size())?, rather than having to keep a separate 'count' variable. (Depending on what implementation this ObservableList class this is.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文