-
Bug
-
Resolution: Not an Issue
-
P4
-
8u20
-
None
-
Windows 7 x64
I want to create a StackPane where, whatever is added, a node of my choice is always at the front (rather than having to be careful about the order I add things in, or remembering to call toFront() on that particular node whenever anything is added.)
In order to do this, I simply place a listener on the child list of the relevant StackPane object, so that whenever anything changes, it calls toFront() on the relevant node, for example:
public class Test extends Application {
@Override
public void start(Stage stage) {
StackPane root = new StackPane();
final Rectangle r1 = new Rectangle(50, 50);
root.getChildren().add(r1);
root.getChildren().addListener(new ListChangeListener<Node>() {
@Override
public void onChanged(ListChangeListener.Change<? extends Node> change) {
try {
while(change.next()) {
if(change.wasAdded()) {
r1.toFront();
}
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
});
root.getChildren().add(new Rectangle(50, 50));
stage.setScene(new Scene(root));
stage.show();
}
}
In JavaFX 2.2, this works just fine. However, in JFX8 (latest build downloaded just now), it fails with the following:
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableList.add(Collections.java:1374)
at javafx.collections.ListChangeBuilder.nextRemove(ListChangeBuilder.java:208)
at javafx.collections.ObservableListBase.nextRemove(ObservableListBase.java:150)
at javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:181)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:284)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:209)
at javafx.scene.Parent.impl_toFront(Parent.java:624)
at javafx.scene.Node.toFront(Node.java:1713)
at test.Test$1.onChanged(Test.java:34)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:315)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:72)
at com.sun.javafx.collections.VetoableListDecorator$1.onChanged(VetoableListDecorator.java:77)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:315)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:72)
at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:155)
at java.util.AbstractList.add(AbstractList.java:108)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:200)
at test.Test.start(Test.java:41)
at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:331)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:297)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)
And yes, test.Test$1.onChanged(Test.java:34) does indeed refer to r1.toFront().
I did wonder whether the list was still being changed while the onChanged() method was executing, and toFront() would also change the list contents, hence the exception - but the Javadoc to onChanged() clearly says:
"Called **after** a change has been made to an ObservableList."
...so I see no reason in the documentation at least why that should prove an issue.
In order to do this, I simply place a listener on the child list of the relevant StackPane object, so that whenever anything changes, it calls toFront() on the relevant node, for example:
public class Test extends Application {
@Override
public void start(Stage stage) {
StackPane root = new StackPane();
final Rectangle r1 = new Rectangle(50, 50);
root.getChildren().add(r1);
root.getChildren().addListener(new ListChangeListener<Node>() {
@Override
public void onChanged(ListChangeListener.Change<? extends Node> change) {
try {
while(change.next()) {
if(change.wasAdded()) {
r1.toFront();
}
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
});
root.getChildren().add(new Rectangle(50, 50));
stage.setScene(new Scene(root));
stage.show();
}
}
In JavaFX 2.2, this works just fine. However, in JFX8 (latest build downloaded just now), it fails with the following:
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableList.add(Collections.java:1374)
at javafx.collections.ListChangeBuilder.nextRemove(ListChangeBuilder.java:208)
at javafx.collections.ObservableListBase.nextRemove(ObservableListBase.java:150)
at javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:181)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:284)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:209)
at javafx.scene.Parent.impl_toFront(Parent.java:624)
at javafx.scene.Node.toFront(Node.java:1713)
at test.Test$1.onChanged(Test.java:34)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:315)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:72)
at com.sun.javafx.collections.VetoableListDecorator$1.onChanged(VetoableListDecorator.java:77)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:315)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:72)
at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:155)
at java.util.AbstractList.add(AbstractList.java:108)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:200)
at test.Test.start(Test.java:41)
at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:331)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:297)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)
And yes, test.Test$1.onChanged(Test.java:34) does indeed refer to r1.toFront().
I did wonder whether the list was still being changed while the onChanged() method was executing, and toFront() would also change the list contents, hence the exception - but the Javadoc to onChanged() clearly says:
"Called **after** a change has been made to an ObservableList."
...so I see no reason in the documentation at least why that should prove an issue.
- relates to
-
JDK-8090935 Throw an exception when a list/set/map is manipulated from within it's *ChangeListener
- Open