简体   繁体   English

toFront()抛出UnsupportedOperationException

[英]toFront() throwing UnsupportedOperationException

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.) 我想创建一个StackPane ,无论添加什么,我选择的节点总是在前面(而不是要小心我添加内容的顺序,或者记住在任何时候调用该特定节点上的toFront()被添加。)

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: 为了做到这一点,我只是将一个监听器放在相关StackPane对象的子列表上,这样无论何时发生任何变化,它都会调用相关节点上的toFront() ,例如:

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 Java 7, this works just fine. 在Java 7中,这很好用。 However, in JFX8 (latest build downloaded just now), it fails with the following: 但是,在JFX8(刚刚下载的最新版本)中,它失败并出现以下情况:

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(); 是的, test.Test$1.onChanged(Test.java:34)确实引用了r1.toFront(); .

Is this to be considered a bug, or am I breaking some rule I'm unaware of by trying to achieve things this way? 这是否被视为一个错误,或者我是否打破了一些我不知道通过这种方式实现事情的规则? 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: 我确实想知道在执行onChanged()方法时列表是否仍然被更改,并且toFront()也会更改列表内容,因此异常 - 但是onChanged()的Javadoc清楚地说:

Called after a change has been made to an ObservableList. 对ObservableList 进行了更改调用。

(Bolding is mine.) (博尔丁是我的。)

EDIT: At this point I'm more certain that it's a bug, so the related bug report is here . 编辑:此时我更确定这是一个错误,所以相关的错误报告就在这里

It seems that you are not allowed to modify a (JavaFX) list inside an event handler that is currently handling another (previous) modification event of the same list. 您似乎不允许修改当前正在处理同一列表的另一个(上一个)修改事件的事件处理程序内的(JavaFX)列表。 Although this seems reasonable it is not self-evident, so there should be a more obvious exception in that case. 虽然这似乎是合理的,但这并不是不证自明的,因此在这种情况下应该有一个更明显的例外。

Unfortunately non-speeking exceptions are very common in JavaFX. 不幸的是,非Speeking异常在JavaFX中非常常见。

Fortunately the solution/workaround is pretty easy: Call your modifying code (here: r1.toFront ) by Platform.runLater() , it will delay your modification to happen after the originating event: 幸运的是,解决方案/解决方法非常简单:通过Platform.runLater()调用修改代码(此处: r1.toFront Platform.runLater() ,它将延迟您的修改发生在原始事件之后:

root.getChildren().addListener(new ListChangeListener<Node>() {
    @Override
    public void onChanged(ListChangeListener.Change<? extends Node> change) {
        Platform.runLater(new Runnable() {
            public void run() { r1.toFront(); }                 
        });
    }
});

Sidenote: toFront does nothing, if the component is already at front. 旁注:如果组件已经在前面, toFront什么toFront做。 This prevents infinite loops. 这可以防止无限循环。 Nevertheless, as this is not explicitely mentioned in the documentation, you might not rely on that. 尽管如此,由于文档中没有明确提及,您可能不会依赖它。

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

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