Italian [B4J] bug nel componente DatePicker?

Gianni M

Well-Known Member
Licensed User
Longtime User
nel layout posiziono il componente DatePicker
poi all'avvio
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    DateTime.DateFormat = "dd/MM/yyyy"
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub
succede che se modifico la data direttamente nella "casella di testo" e non premo invio per la conferma, il valore del DatePicker non cambia.
quindi questo evento non viene invocato:
B4X:
Private Sub DatePicker1_ValueChanged (Value As Long)
    Log(DateTime.Date(Value))
End Sub
è un bug oppure .... ?
 

Gianni M

Well-Known Member
Licensed User
Longtime User
vedi esempio
 

Attachments

  • datario.zip
    2.2 KB · Views: 6

micro

Well-Known Member
Licensed User
Longtime User
Ciao Gianni
che io sappia l'evento DatePicker1_ValueChanged si scatena solo quando cambi il valore da icona calendar del componente.
Potresti usare una TextField da inserire nello spazio di visualizzazione del DatePicker e usare la proprietà Action.
Usa Style per cambiare le proprietà della TextField così da mascherarla bene e all'avvio carichi il testo con il contenuto del DatePicker.
e viceversa.
Naturalmente se proprio vuoi usare anche Enter per aggiornare la Data.
 

LucaMs

Expert
Licensed User
Longtime User
Quale DatePicker?
Mannaggia a me, mi era sfuggito il tuo B4J!

Ho capito il problema, o meglio ciò che vorresti ottenere: modificare la data nella sua casella di testo e, passando ad un'altra view, in questo caso facendo click su un Button, che scattasse l'evento ValueChanged.
Purtroppo usando quel DataPicker non vedo soluzione, in quanto ValueChanged è l'unico evento disponibile; se ci fosse il FocusChanged potresti sfruttare questo.
Non è impossibile che esista un modo tramite JavaObject per aggiungere quest'ultimo evento al DatePicker; tra poco chiederò a qualche IA 😊

Comunque ci sono talmente tanti date picker che peso valga la pena cambiare.
 

LucaMs

Expert
Licensed User
Longtime User
Purtroppo usando quel DataPicker non vedo soluzione, in quanto ValueChanged è l'unico evento disponibile; se ci fosse il FocusChanged potresti sfruttare questo.
Non è impossibile che esista un modo tramite JavaObject per aggiungere quest'ultimo evento al DatePicker; tra poco chiederò a qualche IA 😊
Come non "detto". Grazie a Copilot ho potuto aggiungere l'evento FocusChanged in un attimo, ma ugualmente il DateTicks non viene aggiornato.
 

LucaMs

Expert
Licensed User
Longtime User
Come non "detto". Grazie a Copilot ho potuto aggiungere l'evento FocusChanged in un attimo, ma ugualmente il DateTicks non viene aggiornato.

Risolto.


java_slDBCoPQZf.gif



B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private DatePicker1 As DatePicker
    Private Label1 As Label
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    DateTime.DateFormat = "dd/MM/yyyy"
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
   
    AddFocusListener(DatePicker1, "DatePicker1_FocusChanged")
End Sub

Sub Button1_Click
    Label1.Text = DateTime.Date(DatePicker1.DateTicks)
End Sub

Sub AddFocusListener(view As Node, eventName As String)
    Dim jo As JavaObject = view
    Dim focusedProperty As JavaObject = jo.RunMethod("focusedProperty", Null)

    Dim listener As Object = focusedProperty.CreateEventFromUI( _
        "javafx.beans.value.ChangeListener", eventName, False)

    focusedProperty.RunMethod("addListener", Array(listener))
End Sub

Sub DatePicker1_FocusChanged(HasFocus As Boolean)
    If HasFocus = False Then
        ForceDatePickerCommit(DatePicker1)
        DatePicker1_ValueChanged(DatePicker1.DateTicks)
    End If
End Sub

Sub ForceDatePickerCommit(dp As DatePicker)
    Dim jo As JavaObject = dp
    Dim editor As JavaObject = jo.RunMethod("getEditor", Null)
    Dim text As String = editor.RunMethod("getText", Null)

    Dim converter As JavaObject = jo.RunMethod("getConverter", Null)
    Dim newValue As Object = converter.RunMethod("fromString", Array(text))

    jo.RunMethod("setValue", Array(newValue))
End Sub

Private Sub DatePicker1_ValueChanged (Value As Long)

End Sub
 

Gianni M

Well-Known Member
Licensed User
Longtime User
chatgpt mi ha anche suggerito un controllo sulla validità della data!
B4X:
Sub Button1_Click
    Dim joDP As JavaObject = DatePicker1
    Dim editor As JavaObject = joDP.RunMethod("getEditor", Null)
    Dim txt As String = editor.RunMethod("getText", Null)

    If txt.Length = 0 Then Return

    Try
        Dim ticks As Long = DateTime.DateParse(txt)
        DatePicker1.DateTicks = ticks
        Label1.Text = DateTime.Date(ticks)
    Catch
        Label1.Text = "Data non valida"
    End Try
End Sub
 

Gianni M

Well-Known Member
Licensed User
Longtime User
copilot, gemini, chatgpt: ma come abbiamo fatto negli ultimi 40 anni senza AI e/o internet? :p
 
Top