B4J Code Snippet HTMLEditor add B4J ColorPicker View to Top-Toolbar

Example of adding the B4J ColorPicker View to the Top-Toolbar of the HTML Editor View.
Requires the library JavaObject.
The selected color is copied to the clipboard, use Paste to insert into HTML text.
Project attached, plus another example showing Hyperlink Button.

Add the ColorPicker at AppStart.
B4X:
Sub AppStart (Form1 As Form, Args() As String)
...
  ' Use callsubdelayed to add the button to the right of the top-toolbar
  CallSubDelayed(Me, "AddColorPicker")
...
Sub to add the ColorPicker.
B4X:
Sub AddColorPicker
  Dim joHE As JavaObject = HTMLEditor1
  Dim joTB As JavaObject = joHE.RunMethod("lookup", Array(".top-toolbar"))
  Dim tbColorPicker As ColorPicker
  tbColorPicker.Initialize("tbColorPicker")
  tbColorPicker.ToolTipText = "Select & copy color hex value."
  tbColorPicker.PrefHeight = 26
  ' Set the prefwidth. Default is 52. If making wider, e.g. 150 the color value is also shown
  tbColorPicker.PrefWidth = 52
  joTB.RunMethodJO("getItems", Null).RunMethod("add", Array(tbColorPicker))
End Sub
Handle color selection.
The HexRGB value is copied to the clipboard. Insert into the text by pasting.
B4X:
Sub tbColorPicker_ValueChanged (Value As Paint)
  Dim ColorInt As Int  = fx.Colors.To32Bit(Value)
  Dim HexARGB As String = Bit.ToHexString(ColorInt)
  Dim HexRGB As String = HexARGB.SubString2(2,8)
  fx.Clipboard.SetString($"#${HexRGB}"$)
End Sub

Screenshot
upload_2016-11-27_17-30-16.png


Note
The solution open for enhancements, e.g.
  • creating an event to insert the colorvalue when the colorpicker dialog closes.
  • Other buttons, like add hyperlink, file open / save.
 

Attachments

  • EditorToolbarAddHyperlinkButton.zip
    24.3 KB · Views: 399
  • EditorToolbarAddColorPicker.zip
    2.8 KB · Views: 386

sdleidel

Active Member
Licensed User
Longtime User
I have a question to the Hyperlink exeample.
why does it run in debug, but not in release mode ?

Error:
main._btnhl_action (java line: 149)
java.lang.NoSuchFieldException: webView
    at java.base/java.lang.Class.getDeclaredField(Class.java:2412)
    at anywheresoftware.b4j.agraham.reflection.Reflection.GetField(Reflection.java:409)
    at b4j.example.main._btnhl_action(main.java:149)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
    at anywheresoftware.b4a.BA$1.run(BA.java:233)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
 
Last edited:

sdleidel

Active Member
Licensed User
Longtime User
OK... I found a solution.
I add this:
code:
#VirtualMachineArgs: --add-opens javafx.web/javafx.scene.web=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit.graphics=ALL-UNNAMED --add-opens javafx.web/com.sun.javafx.webkit=ALL-UNNAMED --add-opens javafx.web/com.sun.javafx.webkit.theme=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit.event=ALL-UNNAMED --add-opens javafx.web/com.sun.javafx.webkit.prism=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit.perf=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit.network=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit.plugin=ALL-UNNAMED --add-opens java.xml/org.w3c.dom=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit.dom=ALL-UNNAMED --add-opens java.xml/org.w3c.dom.events=ALL-UNNAMED --add-opens java.xml/org.w3c.dom.ranges=ALL-UNNAMED --add-opens java.xml/org.w3c.dom.traversal=ALL-UNNAMED --add-opens java.xml/org.w3c.dom.views=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit.network.about=ALL-UNNAMED --add-opens javafx.web/com.sun.webkit.network.data=ALL-UNNAMED
 
Top