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 !

Nathan
  • 266
  • 4
  • 16

2 Answers2

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.

Community
  • 1
  • 1
Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76
  • 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 ! – Nathan Jun 02 '15 at 13:35
  • It's better to avoid use of Swing in JavaFX, because SwingNode doesn't works fine with big application :) – Evans Belloeil Jun 02 '15 at 13:51
  • Yeh I've found that out the hard way ! haha, Thanks :) – Nathan Jun 02 '15 at 14:16
  • 1
    I am trying to convert an application that uses jgraphx (based on awt/swing). Any general solution for that? – Mario Stefanutti Mar 18 '16 at 15:42
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
L_J
  • 2,351
  • 10
  • 23
  • 28
Andy
  • 1