B4J Question Disable selectable in Textfield

Star-Dust

Expert
Licensed User
Longtime User
Is it possible to disable the selected in TextField as in B4A and B4i?
 

stevel05

Expert
Licensed User
Longtime User
If I understand you correctly you are trying to stop the user selecting text. If so you can use this code:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TextField1 As TextField
    Private SelMap As Map
    Private TextArea1 As TextArea
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
 
    DisableTextSelection(TextField1)
    DisableTextSelection(TextArea1)
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

Private Sub DisableTextSelection(tf As JavaObject)
    If SelMap.IsInitialized = False Then SelMap.Initialize
    Dim Prop As JavaObject = tf.RunMethod("selectionProperty",Null)
    Dim O As Object = Prop.CreateEventFromUI("javafx.beans.InvalidationListener","SelectionChanged",Null)
    Prop.RunMethod("addListener",Array(O))
    SelMap.Put(Prop,tf)
End Sub

Private Sub SelectionChanged_Event (MethodName As String, Args() As Object) As Object
    Dim JO As JavaObject = SelMap.Get(Args(0))
    Dim IndexRange As JavaObject = JO.RunMethod("getSelection",Null)
    If IndexRange.RunMethod("getStart",Null) <> IndexRange.RunMethod("getEnd",Null) Then
        Dim Pos As Int = JO.RunMethod("getCaretPosition",Null)
        JO.RunMethod("selectRange",Array(Pos,Pos))
    End If
End Sub

Full example app attached, works for TextField and TextArea. The Map is required to select the relevant node from the property. I couldn't find a javafx method of doing this directly.

There are other, slightly more cumbersome ways of doing it if you the internet for "javafx disable text selection"
 

Attachments

  • DisableSelection.zip
    2.3 KB · Views: 180
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Is it possible to disable the selected in TextField as in B4A and B4i?
If

if I understand well what you ask

B4X:
CSSUtils.SetStyleProperty(TextField1, "-fx-highlight-fill", "null")
 CSSUtils.SetStyleProperty(TextField1, "-fx-highlight-text-fill", "null")

Hi to all.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
If I understand you correctly you are trying to stop the user selecting text. If so you can use this code:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TextField1 As TextField
    Private SelMap As Map
    Private TextArea1 As TextArea
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
 
    DisableTextSelection(TextField1)
    DisableTextSelection(TextArea1)
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

Private Sub DisableTextSelection(tf As JavaObject)
    If SelMap.IsInitialized = False Then SelMap.Initialize
    Dim Prop As JavaObject = tf.RunMethod("selectionProperty",Null)
    Dim O As Object = Prop.CreateEventFromUI("javafx.beans.InvalidationListener","SelectionChanged",Null)
    Prop.RunMethod("addListener",Array(O))
    SelMap.Put(Prop,tf)
End Sub

Private Sub SelectionChanged_Event (MethodName As String, Args() As Object) As Object
    Dim JO As JavaObject = SelMap.Get(Args(0))
    Dim IndexRange As JavaObject = JO.RunMethod("getSelection",Null)
    If IndexRange.RunMethod("getStart",Null) <> IndexRange.RunMethod("getEnd",Null) Then
        Dim Pos As Int = JO.RunMethod("getCaretPosition",Null)
        JO.RunMethod("selectRange",Array(Pos,Pos))
    End If
End Sub

Full example app attached, works for TextField and TextArea. The Map is required to select the relevant node from the property. I couldn't find a javafx method of doing this directly.

There are other, slightly more cumbersome ways of doing it if you the internet for "javafx disable text selection"
You both understood my question.

I wanted to prevent the user from selecting the text.
The steve05 solution intercepts the raised event when it is selected.
The second example eliminates the selection color, but still allows you to select.

I need to prevent the copy and paste. The solution of steeve05 is what I was looking for

Thanks to both of you


PS for steve05: Does not SelectionChanged_Event return anything? dovrebeb return an object value
SelectionChanged_Event
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Ok Star-Dust
if you just enter the MouseClicked event you can prevent it
the selection (fix selection to 0)

B4X:
Sub TextFieldx_MouseClicked (EventData As MouseEvent)
    fx.Clipboard.SetString("") 'prevents paste
    TextFieldx.SetSelection(0,0) 'prevents selection
End Sub

This only for a simple and quick solution.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
No the SelectionChanged_Event doesn't return a value. The return type is added automatically by the IDE when creating the sub for a JavaObject, you can remove the As Object in the method definition in this case.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Ok. Thank's
 
Upvote 0
Top