在JavaFX面板中显示GraphStream图形遇线程异常及显示问题求助
JavaFX嵌入GraphStream图形:线程异常与显示过小问题
我需要在JavaFX的StackPane(最小复现示例使用AnchorPane)中显示GraphStream图形,但运行后出现两个问题:
- 图形显示极小且无交互
- 抛出线程异常
初始代码
void cargaGrafico() { SingleGraph graph = aula.getSociogramas().get(currentSocio).getRelaPos().getGraph(); FxViewer v = new FxViewer(graph,FxViewer.ThreadingModel.GRAPH_IN_GUI_THREAD); v.enableAutoLayout(); FxViewPanel panel = (FxViewPanel)v.addDefaultView(false, new FxGraphRenderer()); spGraficos.getChildren().add(panel); }
其中spGraficos是JavaFX StackPane组件。
抛出的异常
Exception in thread "Thread-2" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-2 at javafx.graphics@22-ea/com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:294) at javafx.graphics@22-ea/com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:475) at javafx.graphics@22-ea/javafx.animation.Animation.play(Animation.java:990) at gs.ui.javafx@2.0/org.graphstream.ui.fx_viewer.FxViewer.lambda$init$1(FxViewer.java:220) at java.base/java.lang.Thread.run(Thread.java:1570)
我尝试查找GraphStream邮件列表但已停止维护,下面是最小可复现示例:在IntelliJ IDEA中新建JavaFX项目,仅包含按钮和AnchorPane,点击按钮时填充GraphStream图形并添加到AnchorPane,结果同样出现上述问题。
最小复现示例代码
HelloApplication.java
package com.probags.probags; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.stage.Stage; import java.io.IOException; public class HelloApplication extends Application { @Override public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); Scene scene = new Scene(fxmlLoader.load(), 820, 950); stage.setTitle("Hello!"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { System.setProperty("org.graphstream.ui", "javafx"); launch(); } }
HelloController.java
package com.probags.probags; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.layout.AnchorPane; import org.graphstream.graph.Graph; import org.graphstream.graph.implementations.SingleGraph; import org.graphstream.ui.fx_viewer.FxViewPanel; import org.graphstream.ui.fx_viewer.FxViewer; import org.graphstream.ui.javafx.FxGraphRenderer; public class HelloController { @FXML private AnchorPane apGraph; @FXML private Label welcomeText; private Graph graph = new SingleGraph("Test"); @FXML void onHelloButtonClick(ActionEvent event) { populateGraph(); FxViewer v = new FxViewer(graph,FxViewer.ThreadingModel.GRAPH_IN_GUI_THREAD); v.enableAutoLayout(); FxViewPanel panel = (FxViewPanel)v.addDefaultView(false, new FxGraphRenderer()); apGraph.getChildren().add(panel); } void populateGraph() { graph.addNode("A" ); graph.addNode("B" ); graph.addNode("C" ); graph.addEdge("AB", "A", "B"); graph.addEdge("BC", "B", "C"); graph.addEdge("CA", "C", "A"); } }
module-info.java
module com.probags.probags { requires javafx.controls; requires javafx.fxml; requires gs.core; requires gs.ui.javafx; opens com.probags.probags to javafx.fxml; exports com.probags.probags; }
我使用Maven引入的gs.core和gs.ui.javafx版本为2.0,求解决上述问题。
内容的提问来源于stack exchange,提问作者Serinus Serinus
相关产品推荐
相关产品推荐

