• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Close JAVAFx application in spring project and return its javascript output

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why are you using Integer instead of int?
 
Aditya Soni
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic