在多个视图中观察到的相同列表
我有两个使用相同域对象的视图(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,我不明白您的期望:
要解决此问题,请执行以下操作 :元素一旦在view, fi 通过将侦听器保持在外面:
Actually, I don't understand what you expect:
To solve, do the manipulation of the elements once outside of the view, f.i. by keeping the listener outside:
我认为您可以使用一些内置方法 (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.)