To Integrate JavaFx2 with JavaScript



In order to make a call back in a JavaFX2 application from JavaScript, the first step we need to do is to get  reference to aJavaScript object representing the JavaFX2 application. In Following example show how to execute JavaScript code in JavaFX2 application
// Comment
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
packagejavafxusingjavascript;

importjavafx.application.Application;
importjavafx.application.HostServices;
importjavafx.event.ActionEvent;
importjavafx.event.EventHandler;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.layout.StackPane;
importjavafx.stage.Stage;
importnetscape.javascript.JSObject;

/**
 *
 * @author allinalljava.blogspot.com
 */
public class JavaFxusingJavascript extends Application {
finalHostServices services = getHostServices();
    /**
     * @paramargs the command line arguments
     */
public static void main(String[] args) {
launch(args);
    }

    @Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
        Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler() {

            @Override
public void handle(ActionEvent event) {
              //  System.out.println("Hello World!");
JSObjectjs = services.getWebContext();
js.eval("window.alert('Hello from JavaFX')");
            }
        });

StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
    }
}


OUTPUT VIEW

From above program its shows how to interact webpage with JavaFx 2.0 Program.Here we have use JSObject a class which is responsible for invoking the all JavaScript related function call from java program itself.You can use this class even in your normal applet,servlet program.For using this particular class you should need jdk6.0 and other latest jdk

raja P Web Developer

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Web Development: Web development certifications verify that technicians are trained in C++, HTML/CSS, JavaScript, MySQL, PHP, AJAX and Website Development Professional. Object-oriented (OO) programming certificates, for instance, verify that technicians understand the older platforms.

    javascript jobs

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...