This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Close JAVAFx application in spring project and return its javascript output  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am trying to execute a little JS code which has document object in it. I have already used ScriptEngine but as document doesn't work on it.

Here's my code.

our project is spring xml based configured, in one of my api, i am calling this JavaFx code by passing a html as constructor of Application.

Our requirement is to just get offsetHeight of HTML element through java code.




And one more class which is calling this JavaFxApplication class and passing html String in its constructor.





I am trying to execute a little JS code which has document object in it. I have already used ScriptEngine but as document doesn't work on it.

Here's my code.

our project is spring xml based configured, in one of my api, i am calling this JavaFx code by passing a html as constructor of Application.

Our requirement is to just get offsetHeight of HTML element through java code.

package com.giddh.api.service;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class JavafxSample extends Application {

   String html;
   Integer footerHeight;

   @Override
   public void start(Stage primaryStage) throws Exception {
       Parent root = FXMLLoader.load(this.getClass().getResource("/import/sample.fxml"));
       primaryStage.setTitle("Hello World");
       primaryStage.setScene(new Scene(root, 595, 842));


       final WebView browser = new WebView();
       final WebEngine webEngine = browser.getEngine();

       Parameters parameters = getParameters();
       String html = parameters.getRaw().get(0);
       webEngine.loadContent(html, "text/html");

       primaryStage.show();
       webEngine.getLoadWorker().stateProperty().addListener(
               new ChangeListener<Worker.State>() {
                   @Override
                   public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
                       if (newState == Worker.State.SUCCEEDED) {
                           Integer i = (Integer) webEngine.executeScript(
                                   "var body = document.body, html = document.getElementById(\"Template-footer\"); " +
                                           "Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html" +
                                           ".scrollHeight, html.offsetHeight )");
                           System.out.println("printing footer height == " + i);

                       }
                   }
               });
   }


   public JavafxSample(String html) throws Exception {
       this.html = html;

       launch(html);


   }

   public JavafxSample() {


   }

}
}
And one more class which is calling this JavaFxApplication class and passing html String in its constructor.

  try {
               JavafxSample javafxSample = new JavafxSample(html);

               System.out.println("foooter=="+javafxSample.getFooterHeight());
           } catch (Exception e) {
               e.printStackTrace();
           }
i am unable to close the screen and also unable to set output of JS to class variable.

is there any way by which i can achieve the output value of JS and close this screen and move on to its caller with value.


thanks in advance.
 
Marshal
Posts: 5632
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

... document doesn't work on it.


What does this mean?  You don't get the output you think you should?  Or there is an error?  If so, can you post the stack trace (the full error message)?
 
Knute Snortum
Marshal
Posts: 5632
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why are you using Integer instead of int?
 
Aditya Soni
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reply,


document doesn't work means

that, in ScriptEngine library , when i execute script like this :





it give us error document doesn't defined, i have google for this error and found that in script engine you can get dom object and call offsetheight like property over it.

so i have tried another approach of using JAVAFX to find the offSetHeight so eventually got stucked in this issue of not able to close the window after executing javascript and not able to return its output to its caller function.

 
Knute Snortum
Marshal
Posts: 5632
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aditya Soni wrote:it give us error document doesn't defined


What is the full error message and the stack trace?  (You should include this every time you say, "There was an error.")
 
Aditya Soni
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This error occurred when we tried to find offsetHeight of HTML element using ScriptEngine library of JAVA.

“document” is not defined in <eval> java ScriptEngine




but still didn't got the solution of how to find to offsetHeight of html element without rendering it on browser through java code.

JAVAFX was our second approach , but didn't got success in that approach also, the issue we faced in that was mentioned in my last comment.

thanks for the reply.
 
Knute Snortum
Marshal
Posts: 5632
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get an object of class Document and parse it in JavaFX, without a call to the script engine:
Then do what you need to doc.
 
Screaming fools! It's nothing more than a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!