YXCD
YXCD

Reputation: 23

Adding a JPanel to Javafx

I'm currently working on a signature solution for digital signatures. I received a signature tablet that came with an API. The API is closed and I can't change anything in it. The problem is now for me that I can't handle the reponses of the device. When doing a signature or drawing something, I receive an answer in form of a JPanel. Since I'm programming with JavaFX scenes I now don't know how to include the JPanel into my scene. I only learned to work with JavaFx, so I don't really know a lot about JPanels and stuff.

So.. I wonder if there is a way to add a Jpanel to a FlowPane. All I found about this topic were threads about the other way around.. When people tried to add javafx-Components to Jpanel. But not this way around.. so Maybe it's very obvious how to do it but I don't get it.

JPanel x = pad.startSiganture();
FlowPane flow = new FlowPane();
flow.getChildren.addAll(x);

Basically that's what I would dream of, what of course doesn't work. So.. Thanks for any reponses, I hope its possible.

YXCD

Upvotes: 2

Views: 529

Answers (2)

SDIDSA
SDIDSA

Reputation: 1004

Yes, that is achievable through a SwingNode which is a JavaFX node that can contain swing content, see what the documentation says :

This class is used to embed Swing content into a JavaFX application. The content to be displayed is specified with the setContent(javax.swing.JComponent) method that accepts an instance of Swing JComponent. The hierarchy of components in the JComponent instance should not contain any heavyweight components, otherwise, SwingNode may fail to paint it. The content gets repainted automatically. All the input and focus events are forwarded to the JComponent instance transparently to the developer.

Here is a typical pattern that demonstrates how SwingNode can be used:

public class SwingFx extends Application {

     @Override
     public void start(Stage stage) {
         final SwingNode swingNode = new SwingNode();
         createAndSetSwingContent(swingNode);

         StackPane pane = new StackPane();
         pane.getChildren().add(swingNode);

         stage.setScene(new Scene(pane, 100, 50));
         stage.show();
     }

     private void createAndSetSwingContent(final SwingNode swingNode) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 swingNode.setContent(new JButton("Click me!"));
             }
         });
     }

     public static void main(String[] args) {
         launch(args);
     }
 }

Upvotes: 3

Finn von Holten
Finn von Holten

Reputation: 108

Yes this is possible: Add your JPanel to a SwingNode and then put it into your FlowPane:

SwingNode node = new SwingNode()
node.setContent(pad.startSiganture());
FlowPane flow = new FlowPane();
flow.getChildren.addAll(node);

Upvotes: 4

Related Questions