Resize SwingNode in Pane

2019-08-06 03:55发布

问题:

I'm fairly new to using JavaFX and I am looking to add a JPanel into a JavaFX Pane. The code I currently have works, however the panel is very small. I want to be able to resize it so it fits the JavaFX pane.

Code:

    // Add swing component to JFX
    final SwingNode swingNode = new SwingNode();

    createAndSetSwingContent(swingNode);
    detailPane.getChildren().add(swingNode);

Create Swing content method:

 private void createAndSetSwingContent(final SwingNode swingNode) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            //Create the  viewing pane.
            dataComponentDetailPane = new JEditorPane();
            dataComponentDetailPane.setEditable(false);
            dataDetailView = new JScrollPane(dataComponentDetailPane);

            // Create panel
            mainSwingPanel = new JPanel();
            mainSwingPanel.add(dataDetailView);
            swingNode.setContent(mainSwingPanel);
        }
    });
}

How do I fit the SwingNode/JPanel to fit the size of the JavaFX pane ?

I'm using FMXL to create the Java FX pane. Thanks in advance !

回答1:

I had the same issue than you and there's really a problem between Panel and SwingNode, I don't know exactly why but I have not find the way of using this 2 together.

Right now I have 2 solutions :

  • You can read this and if you use group call : setAutosizeChildren(false) like said the solution.
  • You can implement the SwingNode without using the JPanel, just put it in the JavaFX Pane you have, and it will automatically fits.

If this don't work, post a compilable code.



回答2:

I have the same problem, my solution is:

  1. Use the AnchorPane pane:

 AnchorPane detailPane;

2. create your SwingNode (like you do):


createAndSetSwingContent(swingNode);;                       
// add the following code to make sure the swing node grows with the window.
AnchorPane.setTopAnchor(swingNode, 0d);
AnchorPane.setBottomAnchor(swingNode, 0d);
AnchorPane.setRightAnchor(swingNode, 0d);
AnchorPane.setLeftAnchor(swingNode, 0d);  

detailedPane.getChildren().add(swingNode); // Adding swing node