Why is JavaFX WebEngine getLoadWorker looping?
Asked Answered
T

0

7

I'm not very sure how to word this question but I'll try. My application runs commands against a website with a click of a button. The issue is during each loop the getLoadWorker increases by 1. In the load worker i set listeners. Here is how it works.

   MenuItem executeToHere = new MenuItem("Execute to here");
   executeToHere.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent event) {
            listViewStepItem item = stepListView.getSelectionModel().getSelectedItem();

            int selectedIndex = stepList.getSelectionModel().getSelectedIndex();

    WebBrowser browser = new WebBrowser(item.getWebView(), item.getListView());
        for(int i=0; i < selectedIndex; i++){
            listViewStepItem item2 = stepList.getItems().get(i);
            if(item2.comboBoxSelected.contains("http://")){
               browser.loadURL();
            } else if(item2.comboBoxSelected.contains("enter")){
               browser.enterText();
            } else if(item2.comboBoxSelected.contains("click")){
               browser.click();
            }
        }
            browser.setWorker();
     }
  });





public class WebBrowser{

   public WebBrowser(WebView fxmlWebView, WebEngine webEngine){
      this.view = fxmlWebView;
      this.engine = webEngine;
   }

    public void loadUrl(String url){
        webEngine.load(url);
    }

    public void enterText(){
        System.out.println("ENTER TEXT");
    }

    public void click(){
        System.out.println("click");
    }

    public void setWorker(){
        webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>(){
            public void changed(ObservableValue ov, State oldState, State newState){
                if(newState == javafx.concurrent.Worker.State.SUCCEEDED){

                    listener = new EventListener(){

                        public void handleEvent(org.w3c.dom.events.Event evt) {
                            eventListeners(evt);
                        }
                    };
                    setListenerByTagNames(listener, "a");
                }
            }
        });
    }

private void setListenerByTagNames(EventListener listener, String tagName){
    Document doc = webEngine.getDocument();
    NodeList elements = doc.getElementsByTagName(tagName);
    for(int i=0; i < elements.getLength();i++){
        ((EventTarget) elements.item(i)).addEventListener("click", listener, false);
    }
    System.out.println("Listening on :"+tagName);
}

}

the first time i run it the output looks like this

ENTER TEXT
click
Listening on : a

second time

ENTER TEXT
click
Listening on : a
Listening on : a

third time

ENTER TEXT
click
Listening on : a
Listening on : a
Listening on : a

I don't see how the worker is increasing but it causes the page to reload/refresh somehow and therefore all the changes to the page DOM is reset.

Tanguay answered 8/12, 2015 at 22:18 Comment(8)
You add a new listener every time you call setWorker(). So after you have called it once, it has one listener, after you have called it twice, it has two, etc.Sadiras
But setWorker is not in the loop. What would be a work around?Tanguay
I have tried setting the eventListener outside the WebBrowser class but it is not working.Tanguay
Any idea what can be done to fix the issue?Tanguay
Not without some context for your code: there's no way to tell from what you have posted why the listener is getting added multiple times. You should create a minimal reproducible example and edit your question to include it.Sadiras
I've made the changes to what it currently is in my application. The idea is a user will right click on a ListView item and get a contextMenu they can then click to "Execute to Here" and that will loop all the items in the ListView.Tanguay
please let me know if it doesn't make sense.Tanguay
It seems the webEngine you are passing to WebBrowser constructor by item.getListView() is always the same instance of webEngine. Thus you are adding the change listener unnecessarily multiple times.Parasitology

© 2022 - 2024 — McMap. All rights reserved.