java - toFront() 抛出 UnsupportedOperationException

标签 java javafx javafx-8 java-8

我想创建一个 StackPane ,无论添加什么,我选择的节点总是在前面(而不是必须注意我添加东西的顺序,或者记住每当添加任何内容时调用该特定节点上的 toFront()。)

为了做到这一点,我只是在相关 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();
    }
}

在 Java 7 中,这工作得很好。但是,在 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)

是的,test.Test$1.onChanged(Test.java:34) 确实引用了 r1.toFront();

这是否被视为一个错误,或者我试图以这种方式实现目标是否违反了一些我不知道的规则?我确实想知道在执行 onChanged() 方法时列表是否仍在更改,并且 toFront() 也会更改列表内容,因此异常 - 但onChanged() 的 Javadoc 明确指出:

Called after a change has been made to an ObservableList.

(粗体是我的。)

编辑:此时我更确定这是一个错误,所以相关的错误报告是 here .

最佳答案

似乎不允许您修改当前正在处理同一列表的另一个(先前)修改事件的事件处理程序内的 (JavaFX) 列表。虽然这似乎是合理的,但并不是不证自明的,所以在这种情况下应该有一个更明显的异常(exception)。

不幸的是,非语音异常在 JavaFX 中非常常见。

幸运的是,解决方案/解决方法非常简单:通过 Platform.runLater() 调用您的修改代码(此处:r1.toFront),它会延迟您对在原始事件之后发生:

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

旁注:如果组件已经在前面,toFront 什么都不做。这可以防止无限循环。不过,由于文档中未明确提及这一点,您可能不会依赖它。

关于java - toFront() 抛出 UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21068198/

相关文章:

JavaFX - ListView 不填充 fxml 中的数据

java - 使用按钮创建场景后在场景周围移动多个矩形

javascript - 在 Java 中将用户输入评估为 JavaScript 时的安全风险

Java 获取焦点窗口

java - 为什么将类注释为@Service 不创建 bean?

java - TableView<T> 不显示 ObservableList<T> 对象的内容

javafx 可调整大小的 Pane 和内部的矩形

java - 调用 getClass().getSimpleName() 时在必要的地方留一个空格

java - ObservableList:如何可靠地检测 setAll?

java - 如何构建java桌面应用程序运行时模块化?