Resize SwingNode in Pane
Asked Answered
E

2

2

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 !

Edson answered 2/6, 2015 at 10:51 Comment(0)
P
2

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.

Powwow answered 2/6, 2015 at 12:42 Comment(4)
Thanks for responding, I was trying to re-use custom swing components I created in the panel so was really trying to be lazy, but I've just started re writing them in FX. Thanks again for your comments though !Edson
It's better to avoid use of Swing in JavaFX, because SwingNode doesn't works fine with big application :)Powwow
Yeh I've found that out the hard way ! haha, Thanks :)Edson
I am trying to convert an application that uses jgraphx (based on awt/swing). Any general solution for that?Bunnie
V
0

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
Vaccinate answered 15/8, 2018 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.