Results 1 to 19 of 19
  1. #1
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Problem executing html Script in JavaFX initialize method

    Hey guys,

    I am using a html-file to display Google Maps in a WebEngine in JavaFX.
    When i push a button the method handleUpdate() gets called, which gets a position from another class and updates the marker position.
    What I want is to execute this task in the initialize() method, but if I use webEngine.execute in my initialize() method I get a ton of errors.
    Probably because I also load the engine for the first time in the initialize() method.
    How can I solve this problem?

    Java Code:
    Exception in Application start method
    java.lang.reflect.InvocationTargetException...

    Java Code:
    public class FXMLDocumentController implements Initializable {  
    
        @FXML private TextField Latitude;
        @FXML private TextField Longitude;
        @FXML private WebView view;
        @FXML private WebEngine webEngine;
        final URL urlGoogleMaps = getClass().getResource("Map.html");
    final FileImport GpsData = new FileImport();
    
         @FXML
        public void handleUpdate() throws IOException {
            double ar[] = FileImport.getGpsPosition();
            System.out.println(ar[0] + " " + ar[1]);
            double Ltd = ar[0];
            double Lng = ar[1];
            webEngine.executeScript(""
                    + "window.lat = " + Ltd + ";"
                    + "window.lon = " + Lng + ";"
                    + "document.goToLocation(window.lat, window.lon);"
            );
        }
    
        @Override
        public void initialize(URL url, ResourceBundle rb) {
           
            webEngine = view.getEngine();
            webEngine.load(urlGoogleMaps.toExternalForm());
    
            try {
                double ar[] = FileImport.getGpsPosition();
                System.out.println("Latitude: " + ar[0] + " Longitude: " + ar[1]);
                double Ltd = ar[0];
                double Lng = ar[1];
           
                Latitude.setText("" + Ltd);
                Longitude.setText("" + Lng);  
    
            } catch (IOException ex) {
                Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
    }
    Exceptions:
    Java Code:
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    	at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    	at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    	at java.lang.Thread.run(Thread.java:745)
    Caused by: javafx.fxml.LoadException: 
    file:/C:/Users/Frank/Documents/NetBeansProjects/JavaFXML_5/dist/run242965727/JavaFXML_5.jar!/de/fkfs/v2x/eval/UserInterface.fxml
    
    	at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    	at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    	at de.fkfs.v2x.eval.Main.start(Main.java:22)
    	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    	at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    	at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    	... 1 more
    Caused by: netscape.javascript.JSException: TypeError: undefined is not a function (evaluating 'document.goToLocation(window.lat, window.lon)')
    	at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128)
    	at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
    	at com.sun.webkit.WebPage.executeScript(WebPage.java:1439)
    	at javafx.scene.web.WebEngine.executeScript(WebEngine.java:982)
    	at de.fkfs.v2x.eval.FXMLDocumentController.initialize(FXMLDocumentController.java:153)
    	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    	... 17 more
    Exception running application de.fkfs.v2x.eval.Main
    Java Result: 1
    Thanks!
    Last edited by TomTom1; 07-28-2016 at 01:01 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Problem executing html Script in JavaFX initialize method

    Can you post the exceptions, with their stack traces, and show which line is throwing them?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Problem executing html Script in JavaFX initialize method

    I eddited my first post to include them

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Problem executing html Script in JavaFX initialize method

    I don't see this call in the code you posted:
    at javafx.scene.web.WebEngine.executeScript(WebEngine .java:982)
    at de.fkfs.v2x.eval.FXMLDocumentController.initialize (FXMLDocumentController.java:153)

    Which line is line 153?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Problem executing html Script in JavaFX initialize method

    Line 153 is:
    Java Code:
    webEngine.executeScript(""
                    + "window.lat = " + Ltd + ";"
                    + "window.lon = " + Lng + ";"
                    + "document.goToLocation(window.lat, window.lon);"
            );
    I posted the Code that works, however when I copy this part in my initialize() Method, this error appears

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Problem executing html Script in JavaFX initialize method

    Where is that line located wrt the initialise method?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Problem executing html Script in JavaFX initialize method

    try to add the line

    webEngine.setJavaScriptEnabled(true);

    in the second line of you initialize method and see whether that works. If not, can you post the class that has the FXMLLoader?

  8. #8
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Problem executing html Script in JavaFX initialize method

    @benji2505, I added your code, but still get the same error.

    This is how the code looks like that throws the error (now with webEngine.setJavaScriptEnabled(true)):

    Java Code:
    @Override
        public void initialize(URL url, ResourceBundle rb) {
    
            webEngine = view.getEngine();
            webEngine.setJavaScriptEnabled(true);
            webEngine.load(urlGoogleMaps.toExternalForm());
            Tb_Road.setSelected(true);
    
            try {
                double ar[] = FileImport.getGpsPosition();
                System.out.println("Latitude: " + ar[0] + " Longitude: " + ar[1]);
                double Ltd = ar[0];
                double Lng = ar[1];
    
                Latitude.setText("" + Ltd);
                Longitude.setText("" + Lng);
                
               webEngine.executeScript(""
               + "window.lat = " + updateLoc.getLatitude() + ";"
               + "window.lon = " + updateLoc.getLongitude() + ";"
               + "document.goToLocation(window.lat, window.lon);");
    
            } catch (IOException ex) {
                Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    The FXMLLoader class is too long to post it here, what is the best way to show it to you guys?
    Last edited by TomTom1; 07-28-2016 at 04:28 PM.

  9. #9
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Problem executing html Script in JavaFX initialize method

    I was thinking about the code that actually loads the scene. It should look something like:

    Parent root = FXMLLoader.load(....)

    Also check whether JavaScript is enabled in your browser.
    Right now I am thinking that the bug is inside the xml file or it is an error that's returned from google.

    Also try to replace the line

    webEngine.load(urlGoogleMaps.toExternalForm()); with

    webEngine.load(urlGoogleMaps.toString());

    and see whether that helps, it is a littlebit of a long shot though.
    Last edited by benji2505; 07-28-2016 at 04:30 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Problem executing html Script in JavaFX initialize method

    That's a different bit of Javascript.
    One calls goToLocation and this one ReverseGeo.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Problem executing html Script in JavaFX initialize method

    @Tolls,
    Gosh! Sorry, I also have a button where I use ReverseGeo and copied that, I corrected it.

    @benji2505
    This is mx FXML Main class:
    Java Code:
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Main extends Application {
        
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("UserInterface.fxml"));
            
            Scene scene = new Scene(root);
            
            stage.setScene(scene);
            stage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
        
    }

  12. #12
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Problem executing html Script in JavaFX initialize method

    looks good to me.

    The last question for me was whether the html page actually has the functionality to answer and covers the area that you want to address. Unfortunately the Google Maps API is not my turf, though. I would focus on the error message

    Caused by: netscape.javascript.JSException: TypeError: undefined is not a function

    after that I am out of wisdom - sorry.

  13. #13
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Problem executing html Script in JavaFX initialize method

    Yeah the thing is that it works perfectly by pushing a button with this functionality, however, the same code in the initialize method doesn't work

  14. #14
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Problem executing html Script in JavaFX initialize method

    Mmmmmh - Let's try this:

    Java Code:
    webEngine.getLoadWorker().stateProperty().addListener(
    				new ChangeListener<State>() {
    					public void changed(ObservableValue<? extends State> observable,
    							State oldValue, State newValue) {
    						if(newValue.toString()=="SUCCEEDED"){
    							//your webEngine.executeScript Method here
    						}
    					}
    				});

  15. #15
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Problem executing html Script in JavaFX initialize method

    In the first line it says:
    Java Code:
     "no suitable method found for addListener(<anonymous ChangeListener<java.lang.Thread.State>>) method Observabl.addListener(invalidationListener) is not applicable
    (argument mismacht; <anonymous Changelistener<java.lang.Thread.State>> cannot be converted to InvalidationListener)
    method ObservableValue.addListeenr(ChangeListener<? super javafx.concurrent.Worker.State>) is not applicable
    (argument mismatch; <anonymous ChangeListener<java.lang.Thread.State>> cannort be converted to ChangeListener<? super javafx.concurrent.Workler.State>)
    ...

  16. #16
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Problem executing html Script in JavaFX initialize method

    the error message does not show completely here, so I am not sure what the issue is. I would think it is a syntax error.

    This was my idea: Since your method call worked when it was in a button handler, I thought the issue might occur because the page is not fully loaded when the method is reached under the initialize method. So I tried to slap a Listener onto the stateProperty and when the state changes to "finished loading", the changeListener calls your method.

    The code in #14 works here with a different WebEngine instance.

    Another thought: have you tried to move the webEngine.execute(){..} method into a separate method outside of initialize() and call it from the Main class?
    Last edited by benji2505; 07-28-2016 at 11:58 PM.

  17. #17
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Problem executing html Script in JavaFX initialize method

    I just copied your code and pasted into my program, I will look into that again tomorrow.
    When I try to call the method where I update the position with a button click, I get the same FXMLLoader error again.

  18. #18
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Problem executing html Script in JavaFX initialize method

    Now it's working
    This did the job:

    Java Code:
    webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
                    if (newState == Worker.State.SUCCEEDED) {
                        webEngine.executeScript(""
                                + "window.lat = " + Ltd + ";"
                                + "window.lon = " + Lng + ";"
                                + "document.goToLocation(window.lat, window.lon);");
                    }
    Thanks benji2505

  19. #19
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Problem executing html Script in JavaFX initialize method

    Thank you for getting back to us and your feedback. I'll take a note about the correct Worker.State syntax :)
    My personal impression is that the cooperation between Java and fxml is still a little bumpy here and there, I run into quite a bit of these issues.

Similar Threads

  1. Replies: 1
    Last Post: 06-08-2012, 07:11 AM
  2. JavaFX redirect to pure html webpage
    By bockymurphy in forum JavaFX
    Replies: 3
    Last Post: 05-04-2012, 07:23 PM
  3. Replies: 2
    Last Post: 02-02-2011, 03:51 PM
  4. JavaFX Script and JavaFX Mobile
    By levent in forum Java Software
    Replies: 1
    Last Post: 01-27-2010, 04:48 PM
  5. Replies: 0
    Last Post: 03-23-2009, 04:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •