简体   繁体   English

从Swing元素更新嵌入式JavaFX TableView时堆栈溢出

[英]Stack overflow when updating an embedded JavaFX TableView from Swing elements

In JavaFX 2.2: 在JavaFX 2.2中:

Is there a way to cleanly transfer focus to a Swing component from an embedded JavaFX component, and vice-versa? 有没有办法将焦点从嵌入式JavaFX组件完全转移到Swing组件,反之亦然?

Currently, the only (simple) way I found to transfer focus to the embedded JavaFX component is to call requestFocusInWindow() on the host JFXPanel. 当前,我发现将焦点转移到嵌入式JavaFX组件的唯一(简单)方法是在主机JFXPanel上调用requestFocusInWindow()。 Focus then gets "magically" transferred to the JavaFX (TextArea). 然后将焦点“神奇地”转移到JavaFX(TextArea)。 The problem is, when the JavaFX item in focus loses it to a Swing component, a stack overflow occurs. 问题是,当焦点所在的JavaFX项目将其丢失给Swing组件时,就会发生堆栈溢出。 Checking the stack trace shows the JavaFX CSS processing running infinitely. 检查堆栈跟踪显示JavaFX CSS处理无限运行。 Snippet of which: 其中的片段:

java.lang.StackOverflowError
at com.sun.t2k.LogicalFont.getCanonicalFamilyName(LogicalFont.java:55)
at com.sun.t2k.LogicalFont.getLogicalFont(LogicalFont.java:80)
at com.sun.t2k.T2KFontFactory.createFont(T2KFontFactory.java:338)
at com.sun.javafx.font.PrismFontLoader.font(PrismFontLoader.java:349)
at javafx.scene.text.Font.font(Font.java:143)
at javafx.scene.text.Font.font(Font.java:160)
at com.sun.javafx.css.StyleHelper.lookupFont(StyleHelper.java:2110)
at com.sun.javafx.css.StyleHelper.lookup(StyleHelper.java:1017)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:863)
at javafx.scene.Node.impl_processCSS(Node.java:7418)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
at javafx.scene.Node.impl_reapplyCSS(Node.java:7363)
at javafx.scene.control.Labeled$6.invalidated(Labeled.java:323)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at com.sun.javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:70)
at com.sun.javafx.css.StyleableObjectProperty.applyStyle(StyleableObjectProperty.java:58)
at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:70)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:900)
at javafx.scene.Node.impl_processCSS(Node.java:7418)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
at javafx.scene.Node.impl_reapplyCSS(Node.java:7363)
at javafx.scene.control.Labeled$6.invalidated(Labeled.java:323)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at com.sun.javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:70)
at com.sun.javafx.css.StyleableObjectProperty.applyStyle(StyleableObjectProperty.java:58)
at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:70)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:900)
at javafx.scene.Node.impl_processCSS(Node.java:7418)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
at javafx.scene.Node.impl_reapplyCSS(Node.java:7363)
at javafx.scene.control.Labeled$6.invalidated(Labeled.java:323)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at com.sun.javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:70)
at com.sun.javafx.css.StyleableObjectProperty.applyStyle(StyleableObjectProperty.java:58)
at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:70)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:900)
at javafx.scene.Node.impl_processCSS(Node.java:7418)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
at javafx.scene.Node.impl_reapplyCSS(Node.java:7363)
at javafx.scene.control.Labeled$6.invalidated(Labeled.java:323)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at com.sun.javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:70)
at com.sun.javafx.css.StyleableObjectProperty.applyStyle(StyleableObjectProperty.java:58)
at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:70)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:900)
at javafx.scene.Node.impl_processCSS(Node.java:7418)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
at javafx.scene.Node.impl_reapplyCSS(Node.java:7363)
at javafx.scene.control.Labeled$6.invalidated(Labeled.java:323)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at com.sun.javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:70)
at com.sun.javafx.css.StyleableObjectProperty.applyStyle(StyleableObjectProperty.java:58)
at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:70)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:900)
at javafx.scene.Node.impl_processCSS(Node.java:7418)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
at javafx.scene.Node.impl_reapplyCSS(Node.java:7363)
at javafx.scene.control.Labeled$6.invalidated(Labeled.java:323)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at com.sun.javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:70)
at com.sun.javafx.css.StyleableObjectProperty.applyStyle(StyleableObjectProperty.java:58)
at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:70)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:900)
at javafx.scene.Node.impl_processCSS(Node.java:7418)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
at javafx.scene.Node.impl_reapplyCSS(Node.java:7363)

IIRC, there's a ticket opened in JIRA regarding focus issues between the Swing and JavaFX threads, but I was hoping if someone here is aware of a workaround. IIRC,JIRA中有一张关于Swing和JavaFX线程之间的焦点问题的票据,但是我希望这里的人知道解决方法。

Edit: Turns out it wasn't simply a problem with focus between JavaFX and Swing, but with the interaction between their threads. 编辑:事实证明,这不仅仅是JavaFX和Swing之间的焦点问题,而是它们的线程之间的交互问题。 Looking back when I first posted this, it lacked sufficient detail as to what I wanted to do. 回想当我第一次发布此内容时,它缺少我想做什么的详细信息。

I have a TableView backed by an ObservableList of type Map . 我有一个由Map类型的ObservableList支持的TableView。 I want its contents changed when a Swing component receives focus. 我希望在Swing组件获得焦点时更改其内容。 The update process is properly pushed to the JavaFX thread via Platform.runLater() . 通过Platform.runLater()将更新过程正确地推送到JavaFX线程。 I got it working, but it suddenly started spitting out a stack overflow. 我开始工作了,但是突然间它开始吐出堆栈溢出。

I recently updated to Java 7u71, and thought there was an introduced bug with regard to component focus between Swing and JavaFX, which led to the original title of this question "Transferring focus from embedded JavaFX to Swing" . 我最近更新到Java 7u71,并认为在Swing和JavaFX之间存在有关组件焦点的引入的错误,从而导致了该问题的原标题“将焦点从嵌入式JavaFX转移到Swing” I was able to find the cause of the problem, and posted the answer below. 我能够找到问题的原因,并在下面发布了答案。

I had to dig around a bit more to find the answer, though not deep enough to find out the root cause. 尽管还不够深入,无法找出根本原因,但我还是要多花些时间才能找到答案。

I implemented a customized TableCell (TextFieldTableCell), overriding updateItem() to set the style of the cell. 我实现了自定义的TableCell(TextFieldTableCell),它重写了updateItem()来设置单元格的样式。 I found that even when the update process for the backing ObservableList was passed to the JavaFX thread via Platform.runLater() , the stack overflow was caused by calls to updateItem(), whenever the table data was changed. 我发现,即使将表ObservableList的更新过程通过Platform.runLater()传递给JavaFX线程,只要表数据发生更改,堆栈溢出都是由调用updateItem()引起的。 Passing the style setting to the JavaFX thread, did the trick, like so: 将样式设置传递给JavaFX线程可以达到目的,如下所示:

@Override
public void updateItem(String value, final boolean isEmpty) {
    super.updateItem(value, isEmpty);

    Platform.runLater(new Runnable() {

        @Override
        public void run() {
            if(!isEmpty) {
                    setStyle("-fx-font-weight:bold; -fx-font-size:10");
            }
        }

    });
}

Hope this helps. 希望这可以帮助。

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

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