B4J Question How to pass a value (string) to clipboard in non-UI app?

gvoulg

Member
Licensed User
Longtime User
We cannot use JFX.clipboard in non-UI apps so how can we achieve this functionality?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
send the data to a gui app :)
or maybe its possible to use a library in a non-gui app with clipboard access?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Or maybe use this library
https://www.b4x.com/android/forum/threads/jawtrobot-invoke-keyboard-and-mouse-events-etc.55832/

Or a small example to code it directly.
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
 #CommandLineArgs:
 #MergeLibraries: True
#End Region
Sub Process_Globals
 Dim toolkit As JavaObject
 Dim clipboard As JavaObject
 Dim data As JavaObject
 Dim dataflavor As JavaObject
 Dim stringSelection As JavaObject
 Dim contents As String
End Sub
Sub AppStart (Args() As String)
 ' initialize toolit
 toolkit.InitializeStatic("java.awt.Toolkit")
 ' initialize datatypes
 dataflavor.InitializeStatic("java.awt.datatransfer.DataFlavor")
 ' get the system clipboard
 clipboard = toolkit.RunMethodJO("getDefaultToolkit",Null).RunMethod("getSystemClipboard",Null)
 ' read the data on the clipboard
 data = clipboard.RunMethod("getContents",Array(Null))
 ' get the contents of the clipboard as a string
 contents = data.RunMethod("getTransferData",Array(dataflavor.GetField("stringFlavor")))
 Log("contents : "&CRLF&contents)
 ' create new clipboard data
 stringSelection.InitializeNewInstance("java.awt.datatransfer.StringSelection",Array("hello how are you ?"))
 ' put onto clipboard
 clipboard.RunMethod("setContents",Array(stringSelection,Null))
 ' read the new data on the clipboard to check it changed
 data = clipboard.RunMethod("getContents",Array(Null))
 ' get the contents of the clipboard as a string
 contents = data.RunMethod("getTransferData",Array(dataflavor.GetField("stringFlavor")))
 Log("contents : "&CRLF&contents)
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Or maybe use this library
https://www.b4x.com/android/forum/threads/jawtrobot-invoke-keyboard-and-mouse-events-etc.55832/

Or a small example to code it directly.
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
 #CommandLineArgs:
 #MergeLibraries: True
#End Region
Sub Process_Globals
 Dim toolkit As JavaObject
 Dim clipboard As JavaObject
 Dim data As JavaObject
 Dim dataflavor As JavaObject
 Dim stringSelection As JavaObject
 Dim contents As String
End Sub
Sub AppStart (Args() As String)
 ' initialize toolit
 toolkit.InitializeStatic("java.awt.Toolkit")
 ' initialize datatypes
 dataflavor.InitializeStatic("java.awt.datatransfer.DataFlavor")
 ' get the system clipboard
 clipboard = toolkit.RunMethodJO("getDefaultToolkit",Null).RunMethod("getSystemClipboard",Null)
 ' read the data on the clipboard
 data = clipboard.RunMethod("getContents",Array(Null))
 ' get the contents of the clipboard as a string
 contents = data.RunMethod("getTransferData",Array(dataflavor.GetField("stringFlavor")))
 Log("contents : "&CRLF&contents)
 ' create new clipboard data
 stringSelection.InitializeNewInstance("java.awt.datatransfer.StringSelection",Array("hello how are you ?"))
 ' put onto clipboard
 clipboard.RunMethod("setContents",Array(stringSelection,Null))
 ' read the new data on the clipboard to check it changed
 data = clipboard.RunMethod("getContents",Array(Null))
 ' get the contents of the clipboard as a string
 contents = data.RunMethod("getTransferData",Array(dataflavor.GetField("stringFlavor")))
 Log("contents : "&CRLF&contents)
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub

There is a warning in jdk11.
B4X:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by anywheresoftware.b4j.object.JavaObject (file:/C:/Program%20Files/B4X/B4J/Libraries/JavaObject.jar) to method sun.awt.windows.WToolkit.getSystemClipboard()
WARNING: Please consider reporting this to the maintainers of anywheresoftware.b4j.object.JavaObject
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

The clipboard needs to initialize.
B4X:
clipboard.InitializeStatic("java.awt.datatransfer.Clipboard")
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
working on java 19 - just needed a couple of --add-opens
B4X:
    #VirtualMachineArgs: --add-opens java.desktop/sun.awt.windows=ALL-UNNAMED --add-opens java.desktop/sun.awt.datatransfer=ALL-UNNAMED
 
Upvote 0
Top