Wednesday, January 15, 2014

Integrating JavaFX code to a Swing package

To take advantage of JavaFX such as smooth animation, web views, audio and video playback, and styles based on Cascading Style Sheets (CSS) in your already built Java Swing project, you can either create a JFXPanel to add to your Swing JFrame, or create a JavaFX Application in your Swing package. You need to add these jars (deploy.jar, jfxrt.jar, plugin.jar, javaws.jar) from your JAVA_HOME\jdk folder\jre\lib directory to your project libraries or class path.

1. Creating a JFXPanel and add it to the JFrame. 


import java.awt.BorderLayout;
import java.awt.Dimension;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class NewFXSwingMain extends JFrame {
 
    private final int JFXPANEL_WIDTH_INT = 300;
    private final int JFXPANEL_HEIGHT_INT = 250;
    private JFXPanel fxContainer;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NewFXSwingMain();
            }
        });
    }
 
    public NewFXSwingMain() {
        fxContainer = new JFXPanel();
        fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
     
        // create JavaFX scene
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createScene();
            }
        });
     
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Hello");
        add(fxContainer, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
 
    private void createScene() {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
         //choose one of the panes from javafx.scene.layout
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        fxContainer.setScene(new Scene(root));
    }
}

2. Creating a JavaFX class in your Swing package.


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class NewFXMain extends Application {
 
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
     
        StackPane root = new StackPane();
        root.getChildren().add(btn);
     
        Scene scene = new Scene(root, 300, 250);
     
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
-------------------------------------------------------------------------------------------------------------------------

References:

1.JavaFX for Swing Developers

No comments:

Post a Comment