【问题标题】:Integrating mxgraph inside JavaFX's fxml file在 JavaFX 的 fxml 文件中集成 mxgraph
【发布时间】:2015-12-06 01:16:06
【问题描述】:

我需要制作一个在 JavaFX FXML 文件上生成图形的桌面应用程序。我在 Eclipse 中使用 mxGraph 库来构建图形,并使用 Scene Builder 来编辑 FXML 文件,但我无法使图形显示在 FXML 文件中,它仅在我使用 JFrame 时才有效。 当我这样运行时:

public class mxgraphteste extends JFrame{

public mxgraphteste(){
  mxGraph grafo = new mxGraph();
  Object parent = grafo.getDefaultParent();

  Object v1 = grafo.insertVertex(parent, null, "Brazil", 100, 100, 50, 40);
  Object v2 = grafo.insertVertex(parent, null, "Soccer", 240, 150, 50, 40);
  Object a1 = grafo.insertEdge(parent, null, "loves", v1, v2);

  mxGraphComponent graphComponent = new mxGraphComponent(grafo);
  getContentPane().add(graphComponent);
}

public static void main(String[] args) {
    mxgraphteste frame = new mxgraphteste();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(400, 320);
  frame.setVisible(true);
}

}

它有效。 但我需要图表进入这个:

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.TelaPrincipalController">
    <children>
        <BorderPane fx:id="borderPane" layoutX="446.0" layoutY="141.0" prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <right>
                <Pane fx:id="paineDireita" prefHeight="491.0" prefWidth="126.0" BorderPane.alignment="CENTER">
                    <children>
                        <Button fx:id="buttonNo" layoutX="37.0" layoutY="179.0" mnemonicParsing="false" onAction="#selecionarNo" text="Nó" />
                        <Button fx:id="buttonAresta" layoutX="28.0" layoutY="259.0" mnemonicParsing="false" text="Aresta" />
                    </children>
                </Pane>
            </right>
            <center>
                <Pane fx:id="paineCentro" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" /> **~~~~~~~~~~~~~~~~ I NEED IT GO INSIDE THIS PANE ~~~~~~~~~~~~**
            </center>
        </BorderPane>
    </children>
</AnchorPane>

我已经在使用 jdk8。我尝试关注Oracle's tutorial for Swing Content in JavaFX Applications,但无法将图表转换为 SwingNode。我找到了使用 JFXPanel 的解决方案,用于使用 JavaFX 的 Swing 应用程序,但它并没有起到相反的作用。 有谁知道我做错了什么或者我该如何解决这个问题? 谢谢!

【问题讨论】:

  • Swing GUI 对象应event dispatch thread 上构造和操作,对于example
  • 嘿 Gabriela 我正在从事一个类似的项目,请写信给我; yogonza524@gmail.com 我可以帮你

标签: java swing javafx jgraphx


【解决方案1】:

我不知道mxGraph,所以我只是假设您的代码对于该部分是正确的。

要将 Swing 组件嵌入到 JavaFX 中,您需要将其包装在 SwingNode 中。正如 cmets 中所指出的,您需要在 AWT 事件调度线程上创建摇摆内容。由于 FXMLLoader 在 FX 应用程序线程上运行,您不能直接在 FXML 文件中创建您的 mxGraph,并且必须在控制器类中进行部分初始化。

因此您可以在 FXML 中执行以下操作:

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.TelaPrincipalController">
    <children>
        <BorderPane fx:id="borderPane" layoutX="446.0" layoutY="141.0" prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <right>
                <Pane fx:id="paineDireita" prefHeight="491.0" prefWidth="126.0" BorderPane.alignment="CENTER">
                    <children>
                        <Button fx:id="buttonNo" layoutX="37.0" layoutY="179.0" mnemonicParsing="false" onAction="#selecionarNo" text="Nó" />
                        <Button fx:id="buttonAresta" layoutX="28.0" layoutY="259.0" mnemonicParsing="false" text="Aresta" />
                    </children>
                </Pane>
            </right>
            <center>
                <Pane fx:id="paineCentro" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" >
                    <SwingNode fx:id="swingComponentWrapper"/>
                </Pane>
            </center>
        </BorderPane>
    </children>
</AnchorPane>

现在您可以在控制器中执行以下操作:

public class TelaPrincipalController {

    @FXML
    private SwingNode swingComponentWrapper ;

    public void initialize() {
        SwingUtilities.invokeLater(this::createMxGraph);
    }

    private void createMxGraph() {
        mxGraph grafo = new mxGraph();
        Object parent = grafo.getDefaultParent();

        Object v1 = grafo.insertVertex(parent, null, "Brazil", 100, 100, 50, 40);
        Object v2 = grafo.insertVertex(parent, null, "Soccer", 240, 150, 50, 40);
        Object a1 = grafo.insertEdge(parent, null, "loves", v1, v2);

        mxGraphComponent graphComponent = new mxGraphComponent(grafo);

        swingComponentWrapper.setContent(graphComponent);
    }

    @FXML
    private void selecionarNo() {
        // ...
    }

    // etc etc...
}

请注意,SwingNode 仅适用于 轻量级 组件(即其绘制由 Swing 管理的组件);如果 mxGraph 使用重量级组件,这可能不起作用。

【讨论】:

  • 您使用的是哪个版本的 jgraphx?您还可以发布一个完整的工作示例以更好地理解解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 2020-07-24
  • 1970-01-01
相关资源
最近更新 更多