This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

Aditya Soni

Greenhorn
+ Follow
since Oct 23, 2016
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
3
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Aditya Soni

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.
1 year ago
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.

1 year ago

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.
1 year ago
Thanks for replying,

So there are two more questions arise on mind after reading your answer i.e.

1) Why we can't we use "this" instead of private "lock" reference ??
2) And is calling notify will release the lock also? so as another thread will accquire the lock and continue its work?


hope you
so you mean to say calling notify() method won't release lock.?

correct me if am wrong!



Here It is showing ouptut:-
i=0in t1
i=1in t4
Thread-0is running
Thread-1is notifying
notified

but why after printing "notified" it doesn't loose lock to thread "t1 run() method " and continue with the code after wait() in t1 . i.e it should print "Thread-0is waken up" after printing "notified".
Oh really, anyway i got some learning.
Thanks all for the answers.
3 years ago
okay now i got it.

your code is a bit tough to understand for me at first but i will go thru it again.

and yeah i am just gaining knowledge about Singleton Concept as it is very important perspective in interview purposes
3 years ago




Is this correct design for a singleton class?
Is object here is single and same everytime?
3 years ago
Thank you so much for your effort to make me understand what exactly is singleton and factory methods.
Now i got it the difference but, as you stated that,


Junilu Lacar wrote:

Again, your code does not have a singleton. You only have factory methods that produce single objects each time you call them.



There is what i am stucking again.

If my code for singleton method is wrong then,
What is the correction i have to do to achieve singleton class that has method which return only SINGLE PENNY(OBJECT) each time.
as i have read this on (http://www.javatpoint.com/singleton-design-pattern-in-java&grqid=C71jFPsN&hl=en-IN) about design patterns of singleton.
The code i have written is i get from here.

Please correct me for the write code for singleton class?(easy go )
Thanks again all for the efforts.


3 years ago
so finally i am stating that:-
in the method getSingleObject() which "static" is returning me a single object everytime i make a call to it.
and the method multipleObject() which is not static is returning new object everytime i make i make a call to it.

is that correct?
3 years ago
Guys,

Firstly please solve my silly doubt i.e,
Why there is same hashcode in every method calling? or It meant to be different objects but JVM store the object to that hascode everytime?
3 years ago
Hi all,

I have created a Singleton Class here.



OUTPUT:1284693
It outputs the same hashcode everytime and yes it is Valid as according to singleton concept but,
The next Class which is defined below,


OUTPUT: 1284693
this also outputs same hashcode why?

question 1) i have not used static modifier in the method here then also it is returning the same object?

question 2) If this code is wrong then, how to create a method that return new object everytime?

thanks!
3 years ago

MaheshS Kumbhar wrote:
In above code sample we are actually accessing static method test. How does it work as we cant access static methods with super keyword?

3 years ago