B4J Question Webview

Sylou

Member
Licensed User
Longtime User
I would like to change the background color of a WebView
before loading Html
I tried in the CSS of the WebView, it doesn't work
I loaded a web page before, still nothing
With the help !!
 
Solution
Alternate

Code:
    wv.Initialize("wv")
    MainForm.RootPane.AddNode(wv,100,200,100,100)
    wv.SetSize(100,100)
    Dim colour As JavaObject
    colour.Initializestatic("javafx.scene.paint.Color")
    wv.As(JavaObject).RunMethod("setPageFill",Array(colour.GetField("GREEN")))
    wv.LoadHtml("") ' << important wont show colour till something added to wv
Désolé, je viens de trouver une réponse de @Erel

Code:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Charger le fichier de mise en page.
    MainForm.Afficher
    WebView1.LoadUrl("https://www.google.com")
    Attendre la fin de la page WebView1_PageFinished (URL sous forme de chaîne)
    ExécuterJavaScript(WebView1, $"document.body.style.backgroundColor = '#F00'"$)
Fin de la sous-routine

Sub ExecuteJavaScript (wv As WebView, script As String) As Object
    Dim jo As JavaObject = wv
    Retourner jo.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(script))
Fin Sub
 
Upvote 0
Alternate

Code:
    wv.Initialize("wv")
    MainForm.RootPane.AddNode(wv,100,200,100,100)
    wv.SetSize(100,100)
    Dim colour As JavaObject
    colour.Initializestatic("javafx.scene.paint.Color")
    wv.As(JavaObject).RunMethod("setPageFill",Array(colour.GetField("GREEN")))
    wv.LoadHtml("") ' << important wont show colour till something added to wv
 
Upvote 0
Solution
Désolé, je viens de trouver une réponse de @Erel

Code:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Charger le fichier de mise en page.
    MainForm.Afficher
    WebView1.LoadUrl("https://www.google.com")
    Attendre la fin de la page WebView1_PageFinished (URL sous forme de chaîne)
    ExécuterJavaScript(WebView1, $"document.body.style.backgroundColor = '#F00'"$)
Fin de la sous-routine

Sub ExecuteJavaScript (wv As WebView, script As String) As Object
    Dim jo As JavaObject = wv
    Retourner jo.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(script))
Fin Sub
HTML:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private WebView1 As WebView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    Dim html As String
    html=$"
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body {
    background-color: lightblue;
    }
    </style>
    </head>
    <body>
    <h1>Welcome!</h1>
    <p>This My color is light blue</p>
    </body>
    </html>
    "$
    WebView1.LoadHtml(html)
End Sub
 
Upvote 0
Back
Top