0

There's a button btn in my JavaFX GUI interface. When clicking btn, a graph will be drawn.

But when I close the graph, my JavaFX GUI will auto-close as well, which is not desired.

How can I prevent the JavaFX GUI from auto-close?

If the solution involve embedding the generated graph inside the JavaFX GUI, I am also fine. Thanks!

import javax.swing.JFrame;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MainFx extends Application {
    
    
    @Override
    public void start(Stage primaryStage) {
    Button btn = new Button();
    btn.setText("Draw Graph");
    btn.setOnAction(new EventHandler<ActionEvent>() {
        
        // draw graph when clicking the button
        @Override
        public void handle(ActionEvent event) {
                
        JGraphXLearning1 frame = new JGraphXLearning1();    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setVisible(true);
        
        }
    });
    
    StackPane root = new StackPane();
    root.getChildren().add(btn);
    
    Scene scene = new Scene(root, 300, 250);
    
    primaryStage.setScene(scene);
    primaryStage.show();
    }
 
    public static void main(String[] args) {
    launch(args);
    }
 
    public class JGraphXLearning1 extends JFrame {
                 
          public JGraphXLearning1() {
            super("JGraphXLearning1");
            mxGraph graph = new mxGraph();
            Object defaultParent = graph.getDefaultParent();
            graph.getModel().beginUpdate();
            Object v1 = graph.insertVertex(defaultParent, null, "Hello", 20, 20, 80, 30);
            Object v2 = graph.insertVertex(defaultParent, null, "World", 240, 150, 80, 30);
            graph.insertEdge(defaultParent, null, "Edge", v1, v2);
            graph.getModel().endUpdate();
            mxGraphComponent graphComponent = new mxGraphComponent(graph);
            getContentPane().add(graphComponent);
          }

        }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
H42
  • 725
  • 2
  • 9
  • 28
  • 4
    Dont call `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)`? Note that you should be showing the `JFrame` on the AWT event dispatch thread, not on the FX Application thread, so the code in your button's handler really should be wrapped in `SwingUtilities.invokeLater(...)`. (Avoid mixing UI toolkits whenever possible.) – James_D Jul 22 '20 at 20:09
  • 1
    Mixing Swing and JavaFX adds a fair amount of complexity. As much as I’m a fan of JavaFX, in this case I would remove it entirely from the program, and either make `btn` a JButton, or replace it with a simple JOptionPane. – VGR Jul 22 '20 at 22:24
  • @VGR, Thanks for comment. I don't exactly get your meaning. For "remove it entirely" do you mean to remove the JFrame entirely, or to remove the JavaFX stuff entirely? To use JOptionPane do you mean to embed JavaFX inside JFrame, or to embed JFrame inside JavaFX? – H42 Jul 23 '20 at 17:51
  • 1
    Remove JavaFX entirely. You don’t need it. Instead of a Stage, make a JFrame; instead of a JavaFX Button, make a JButton and call its addActionListener method. – VGR Jul 23 '20 at 18:51

0 Answers0