Question

I'm learning JavaFX, trying to write a simple browser, but how do I program the Back and Forward buttons in JavaFX with WebView and WebEngine ? Any sample code ?

Was it helpful?

Solution 2

I figured it out :

  public String goBack()
  {    
    final WebHistory history=eng.getHistory();
    ObservableList<WebHistory.Entry> entryList=history.getEntries();
    int currentIndex=history.getCurrentIndex();
//    Out("currentIndex = "+currentIndex);
//    Out(entryList.toString().replace("],","]\n"));

    Platform.runLater(new Runnable() { public void run() { history.go(-1); } });
    return entryList.get(currentIndex>0?currentIndex-1:currentIndex).getUrl();
  }

  public String goForward()
  {    
    final WebHistory history=eng.getHistory();
    ObservableList<WebHistory.Entry> entryList=history.getEntries();
    int currentIndex=history.getCurrentIndex();
//    Out("currentIndex = "+currentIndex);
//    Out(entryList.toString().replace("],","]\n"));

    Platform.runLater(new Runnable() { public void run() { history.go(1); } });
    return entryList.get(currentIndex<entryList.size()-1?currentIndex+1:currentIndex).getUrl();
  }

OTHER TIPS

if you don't need to get or set any indices, here is a concise way with javascript to code the back and forward buttons for a custom contextMenu:

public void goBack() {
    Platform.runLater(() -> {
        webEngine.executeScript("history.back()");
    });
}

public void goForward() {
    Platform.runLater(() -> {
        webEngine.executeScript("history.forward()");
    });
}

The code under "I figured it out" is a good example of how to code the buttons, except that if you run it as is, it throws out of bounds exceptions. This happens if the user clicks back when the WebEngine browser first loads, for instance. In that case, the entryList has a length of 1, and calling history.goBack(-1) tries to access the entryList at its current position minus 1 (i.e., 0 - 1), which is out of bounds. A similar situation exists for calling history.go(1) for goForward when the currentIndex is already the end of the entryList, in which case the call tries to access the list at an index beyond its length, again out of bounds.

The simple sample code below deals with the bounds of the entry list at any point in time:

public void goBack()
{ 
  final WebHistory history = webEngine.getHistory();
  ObservableList<WebHistory.Entry> entryList = history.getEntries();
  int currentIndex = history.getCurrentIndex();

  Platform.runLater(() -> 
  {
    history.go(entryList.size() > 1 
      && currentIndex > 0
            ? -1
            : 0); 
  });        
}

public void goForward()
{
  final WebHistory history = webEngine.getHistory();   
  ObservableList<WebHistory.Entry> entryList = history.getEntries();
  int currentIndex = history.getCurrentIndex();

  Platform.runLater(() -> 
  {
    history.go(entryList.size() > 1
      && currentIndex < entryList.size() - 1
                    ? 1
                    : 0); 
  });    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top