简体   繁体   中英

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

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);

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 a Swing content into a JavaFX application.The content to be displayed is specified with the setContent(javax.swing.JComponent) methodthat accepts an instance of Swing JComponent. The hierarchy of componentscontained in the JComponent instance should not contain any heavyweightcomponents, otherwise SwingNode may fail to paint it. The content getsrepainted automatically. All the input and focus events are forwarded to the JComponent instance transparently to the developer.

Here is a typical pattern which 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);
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM