Question

How do you convert Jpanel to a regular JavaFX program?

How do you convert Jpanel to a regular JavaFX program?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SwingNode. Yes, SwingNode class present in javafx.embed.swing.SwingNode is used to embed a Swing content into a JavaFX application. As JPanel is a component of Java Swing. So to use Swing components in JavaFX we have SwingNode class in javafx.

Below JavaFX example program is showing how to use JPanel in JavaFX.

import javax.swing.JLabel;
import javax.swing.JPanel;

import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class JavaFXAndSwingNodeExample extends Application{

   public void start(Stage stage) throws Exception{
       try{
           final SwingNode swingNode = new SwingNode();
     
           addJPanelToSwingNode(swingNode);

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

   stage.setScene(new Scene(pane, 100, 50));
   stage.setMaximized(true);
   stage.show();
       }
       catch (Exception e){
           throw e;
       }
   }

   private void addJPanelToSwingNode(SwingNode swingNode) {
       JPanel panel = new JPanel();
       JLabel jLabel = new JLabel("Exaple to use Jpanel in JavaFX");
       panel.add(jLabel);
swingNode.setContent(panel);      
   }

   public static void main(String[] args){
       try{
           launch(args);
       }
       catch (Exception e){
           e.printStackTrace();
       }
   }
}

Screenshot of above program:-

Output of above program:-

Add a comment
Know the answer?
Add Answer to:
How do you convert Jpanel to a regular JavaFX program?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT