为什么我只能在应用程序启动调用中初始化JavaFX HTMLEditor?

时间:2014-11-07 05:37:45

标签: java javafx javafx-2 html-editor

如果我放置此代码

new HTMLEditor();

除了在我的Application.start(阶段阶段)实现中,我收到以下错误:

SEVERE: javafx.scene.control.Control loadSkinClass Failed to load skin 'StringProperty [bean: HTMLEditor[id=null], name: skinClassName, value: com.sun.javafx.scene.web.skin.HTMLEditorSkin]' for control HTMLEditor[id=null]
java.lang.IllegalStateException: Not on FX application thread; currentThread = main
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:238)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:400)
    at javafx.scene.Scene.<init>(Scene.java:293)
    at javafx.scene.Scene.<init>(Scene.java:201)
    at javafx.stage.PopupWindow.<init>(PopupWindow.java:110)
    at javafx.scene.control.PopupControl.<init>(PopupControl.java:98)
    at javafx.scene.control.Tooltip.<init>(Tooltip.java:143)
    at com.sun.javafx.scene.web.skin.HTMLEditorSkin.addButton(HTMLEditorSkin.java:785)
    at com.sun.javafx.scene.web.skin.HTMLEditorSkin.populateToolbars(HTMLEditorSkin.java:470)
    at com.sun.javafx.scene.web.skin.HTMLEditorSkin.<init>(HTMLEditorSkin.java:235)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at javafx.scene.control.Control.loadSkinClass(Control.java:1044)
    at javafx.scene.control.Control.access$500(Control.java:70)
    at javafx.scene.control.Control$12.invalidated(Control.java:972)
    at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:127)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:161)
    at com.sun.javafx.css.StyleableStringProperty.set(StyleableStringProperty.java:71)
    at javafx.scene.control.Control$12.set(Control.java:964)
    at com.sun.javafx.css.StyleableStringProperty.applyStyle(StyleableStringProperty.java:59)
    at com.sun.javafx.css.StyleableStringProperty.applyStyle(StyleableStringProperty.java:31)
    at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:70)
    at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:76)
    at javafx.scene.web.HTMLEditor.<init>(HTMLEditor.java:29)
    at com.vasolsim.tclient.TeacherClient.initHTMLEditor(TeacherClient.java:134)
    at com.vasolsim.tclient.element.form.QuestionInitNode.redrawNode(QuestionInitNode.java:53)
    at com.vasolsim.tclient.element.form.QuestionInitNode.<init>(QuestionInitNode.java:33)
    at com.vasolsim.tclient.TeacherClient.initializeStaticAssets(TeacherClient.java:172)
    at com.vasolsim.tclient.TeacherClient.main(TeacherClient.java:183)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

当我在start方法中创建对象时,一切正常。

有没有人有任何想法?

谢谢, - 将会

1 个答案:

答案 0 :(得分:2)

您需要使用FX应用程序线程(在启动Application时创建)以初始化FX对象。

如果您需要在运行时期间在Application代码之外初始化此类对象,则可以将代码包装在Platform.runLater()调用中。

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        // add your init here
    }
});

这将安排您的任务在下次执行时在FX应用程序线程上运行(实际上,在将来某个未指定的时间......)。 See reference