Android Tutorial Example of B4J control - WebViewWrapper

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code is the code of WebViewWrapper:
B4X:
package anywheresoftware.b4j.objects;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.scene.web.WebView;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4j.objects.PaneWrapper.NativeAndWrapper;

/**
 * WebView is an embedded browser.
 *LocationChanged event is raised whenever the current location changes.
 *PageFinished event is raised after the page is completely loaded.
 */
@ShortName("WebView")
@Events(values={"LocationChanged (Location As String)", "PageFinished (Url As String)"})
public class WebViewWrapper extends NodeWrapper<WebView>{
   static {
     //This line ties the native WebView with this wrapper. It is required for the integration with Scene Builder (FXML layouts).
     PaneWrapper.nativeToWrapper.addFirst(new NativeAndWrapper(WebView.class, WebViewWrapper.class));
   }
   @Override
   @Hide
   public void innerInitialize(final BA ba, final String eventName, boolean keepOldObject) {
     if (!keepOldObject)
       setObject(new WebView());
     super.innerInitialize(ba, eventName, true);
     if (ba.subExists(eventName + "_locationchanged")) {
       getObject().getEngine().locationProperty().addListener(new ChangeListener<String>() {

         @Override
         public void changed(ObservableValue<? extends String> arg0,
             String arg1, String arg2) {
           ba.raiseEventFromUI(getObject(), eventName + "_locationchanged", arg2);
         }
       });
     }
     if (ba.subExists(eventName + "_pagefinished")) {
       getObject().getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {

         @Override
         public void changed(ObservableValue<? extends State> arg0,
             State arg1, State arg2) {
           if (arg2 == State.SUCCEEDED) {
             ba.raiseEventFromUI(getObject(), eventName + "_pagefinished", getObject().getEngine().getLocation());
           }
         }
       });
     }
   }
   /**
    * Asynchronously loads the given url.
    */
   public void LoadUrl(String Url) {
     getObject().getEngine().load(Url);
   }
   /**
    * Asynchronously loads the given html string.
    */
   public void LoadHtml(String HtmlString) {
     getObject().getEngine().loadContent(HtmlString);
   }
   
   /**
    * Gets or sets the control preferred height.
    */
   public double getPrefHeight() {
     return getObject().getPrefHeight();
   }
   public void setPrefHeight(double d) {
     getObject().setPrefHeight(d);
   }
   /**
    * Gets or sets the control preferred width.
    */
   public double getPrefWidth() {
     return getObject().getPrefWidth();
   }
   public void setPrefWidth(double d) {
     getObject().setPrefWidth(d);
   }
   
}

In order to build a B4J library you need to reference jCore.jar and jFX (for UI library).
WebView is not a subclass of Control so we need to inherit from NodeWrapper. If it was a control then it would have been better to inherit from ControlWrapper (or PaneWrapper for Panes).

Pay attention to the static constructor:
static {
//This line ties the native WebView with this wrapper. It is required for the integration with Scene Builder (FXML layouts).
PaneWrapper.nativeToWrapper.addFirst(new NativeAndWrapper(WebView.class, WebViewWrapper.class));
}
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…