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.

Salim
  • 199
  • 3
  • 18
  • 1
    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. – James_D Dec 08 '15 at 23:15
  • But setWorker is not in the loop. What would be a work around? – Salim Dec 09 '15 at 00:38
  • I have tried setting the eventListener outside the WebBrowser class but it is not working. – Salim Dec 09 '15 at 15:59
  • Any idea what can be done to fix the issue? – Salim Dec 15 '15 at 14:40
  • 1
    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 [MCVE] and [edit] your question to include it. – James_D Dec 15 '15 at 14:41
  • 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. – Salim Dec 15 '15 at 16:48
  • please let me know if it doesn't make sense. – Salim Dec 15 '15 at 19:35
  • 2
    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. – Uluk Biy Dec 19 '15 at 04:54

0 Answers0