gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:55 29 4
gpt4 key购买 nike

在 JavaFX 2.2 中:

有没有一种方法可以将焦点从嵌入式 JavaFX 组件干净地转移到 Swing 组件,反之亦然?

目前,我发现将焦点转移到嵌入式 JavaFX 组件的唯一(简单)方法是在主机 JFXPanel 上调用 requestFocusInWindow()。然后焦点“神奇地”转移到 JavaFX(TextArea)。问题是,当焦点中的 JavaFX 项失去焦点并转移到 Swing 组件时,就会发生堆栈溢出。检查堆栈跟踪显示 JavaFX CSS 处理无限运行。其中片段:

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,JIRA 中开出了一张关于 Swing 和 JavaFX 线程之间焦点问题的票证,但我希望这里有人知道解决方法。

编辑:事实证明,这不仅仅是 JavaFX 和 Swing 之间的焦点问题,而是它们线程之间的交互问题。回顾我第一次发布这篇文章时,它缺乏足够的细节来说明我想要做什么。

我有一个由 Map 类型的 ObservableList 支持的 TableView。我希望当 Swing 组件获得焦点时更改其内容。更新过程通过 Platform.runLater() 正确推送到 JavaFX 线程。我让它工作了,但它突然开始吐出堆栈溢出。

我最近更新到 Java 7u71,并认为 Swing 和 JavaFX 之间的组件焦点存在引入的错误,这导致了这个问题的原始标题“将焦点从嵌入式 JavaFX 转移到 Swing” 。我找到了问题的原因,并在下面发布了答案。

最佳答案

我必须进一步挖掘才能找到答案,尽管还不够深入,无法找出根本原因。

我实现了一个自定义的TableCell (TextFieldTableCell),重写updateItem()来设置单元格的样式。我发现,即使支持 ObservableList 的更新过程通过 Platform.runLater() 传递到 JavaFX 线程,只要表数据发生更改,堆栈溢出也是由调用 updateItem() 引起的。将样式设置传递给 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");
}
}

});
}

希望这有帮助。

关于java - 从 Swing 元素更新嵌入式 JavaFX TableView 时堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788263/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com