B4J Question [SOLVED] How i can get checkbox value with javaobject?

Magma

Expert
Licensed User
Longtime User
Hi there...

How i can get checkbox value with javaobject?

I know these:
B4X:
'This will log datepicker value actually the real date not the ticks.. better for my situation
log(jo.RunMethod("getValue", Null))

'This will log textfield value
log(jo.RunMethod("getText", Null))

But how i can see if the checkbox has been checked or not with javaobject not with .checked ?

I want it because i am creating some nodes at running time..
 

Magma

Expert
Licensed User
Longtime User
Upvote 0

Magma

Expert
Licensed User
Longtime User
And how can i read all nodes to get their values - the best way...

For now i was using this:
B4X:
    For Each n As Node In Pane1.GetAllViewsRecursive
       
        Dim jo As JavaObject
        jo=n
       
        If n Is TextField Then

....
blah blah blah

what can i do for reading them ???
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
@Erel i am creating nodes on the fly with same name (just changing tag) so i need check all nodes with javaobject and then check their values to assign them at db fields.... something like automatic form for db.
You can check the value without Java object assigning the sender to the checked control in the Checkedchanged event
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
@Erel

B4X:
Sub Class_Globals
    Private fx As JFX
    Private frm1 As Form
    Private ScrollPane1 As ScrollPane
    Private saveit As Button
    Private cancelit As Button
    Private Pane1 As Pane
  
    Dim lb As Int
    Dim et As Int
    Dim dt As Int
    Dim y As Int
    Dim wtype As Int=0
    Dim fid As String
    Dim scrolly As Int=40

End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Parent As Form)
    frm1.Initialize("frm",600dip,600dip)
    frm1.Resizable=True
    frm1.RootPane.LoadLayout("newform")
    frm1.SetFormStyle("UTILITY")
    frm1.Title=Main.formtitle
          
    frm1.SetOwner(Parent)
  
    ScrollPane1.LoadLayout("newpane",-1, scrolly)

    y=10
  
  
      
Select Case Main.formjob

  
Case "NEWPEL"
    createtextfield("ID:","id",True,False)
    createtextfield("Name:","pelaths",False,True)
    createcheckfield("Check if want to enable:","chk", True)  
End Select
  
  
    ScrollPane1.innernode.SetSize(Pane1.Width,scrolly)
  
  
End Sub

Sub createcheckfield(lbl As String,fieldname As String, isenabled As Boolean)
    Dim mylabel As Label
    mylabel.Initialize("mylbl")
    mylabel.Text=lbl
    CSSUtils.SetStyleProperty(mylabel,"-fx-alignment","center-left")
    Dim mytext As CheckBox
    mytext.Initialize("mytxt")
    mytext.Text=""
    mytext.Tag=fieldname
    If isenabled=False Then
        mytext.Enabled=False
    End If
    Pane1.AddNode(mylabel,10,y,250,25)
    Pane1.AddNode(mytext,270,y,280,25)
    y=y+35


  
End Sub


Sub createtextfield(lbl As String,fieldname As String,no As Boolean, isenabled As Boolean)
    Dim mylabel As Label
    mylabel.Initialize("mylbl")
    mylabel.Text=lbl
    CSSUtils.SetStyleProperty(mylabel,"-fx-alignment","center-left")
    Dim mytext As TextField
    mytext.Initialize("mytxt")
    mytext.PromptText=""
    mytext.Tag=fieldname
    If no=True Then
        CSSUtils.SetStyleProperty(mytext,"-fx-alignment","center-right")
    End If
    If isenabled=False Then
        mytext.Enabled=False
    End If
    Pane1.AddNode(mylabel,10,y,250,25)
    Pane1.AddNode(mytext,270,y,280,25)
    y=y+35
    scrolly=scrolly+35

End Sub

Sub createdatefield(lbl As String,fieldname As String, isenabled As Boolean)
    Dim mylabel As Label
    mylabel.Initialize("mylbl")
    mylabel.Text=lbl
    CSSUtils.SetStyleProperty(mylabel,"-fx-alignment","center-left")
    Dim mytext As DatePicker
    mytext.Initialize("mytxt")
    mytext.Tag=fieldname
    If isenabled=False Then
        mytext.Enabled=False
    End If
    Pane1.AddNode(mylabel,10,y,250,25)
    Pane1.AddNode(mytext,270,y,280,25)
    y=y+35
    scrolly=scrolly+35

End Sub

Public Sub Show As String
    frm1.showandwait

    Return "OK"
End Sub

Sub frm_CloseRequest (EventData As Event)
    'EventData.Consume
End Sub

Sub saveit_Action
    Select Case Main.formjob
        Case "NEWPEL"
            Dim wvalues As String
            wvalues=getfields
            'Main.sql1.ExecNonQuery("INSERT INTO pelates " & wvalues & ";")
            Log(wvalues)
            cancelit_Action
    End Select
End Sub


Sub readfields
    Dim cur As ResultSet

    If IsNumber(Main.recnow)=True Then
        wtype=1  
    End If
  
    cur=Main.sql1.ExecQuery("SELECT * from " & Main.tablenow & ";")
    fid = cur.GetColumnName(0)
    cur.Close  


    Log(fid)
    Log(Main.recnow)
    Log(wtype)
  
    If wtype=1 Then
    cur=Main.sql1.ExecQuery("SELECT * from " & Main.tablenow & " WHERE " & fid & "=" & Main.recnow & " LIMIT 1;")
    Else
    cur=Main.sql1.ExecQuery("SELECT * from " & Main.tablenow & " WHERE " & fid & "='" & Main.recnow  & "' LIMIT 1;")
    End If
  

    Do While cur.nextrow
    For Each n As Node In Pane1.GetAllViewsRecursive
      
        Dim jo As JavaObject
        jo=n
        If n.Tag<>"" Then

        If n Is TextField Then
                If CSSUtils.GetStyleProperty(n,"-fx-alignment")="center-right" Then
                    For k=0 To cur.ColumnCount-1
                    If cur.GetColumnName(k)=n.Tag Then
                    jo.RunMethod("setText", Array As Object(cur.GetString2(k)))
                    n=jo
                    Exit
                    End If
                    Next
                Else
                    For k=0 To cur.ColumnCount-1
                    If cur.GetColumnName(k)=n.Tag Then
                    jo.RunMethod("setText", Array As Object(cur.Getstring2(k)))
                    n=jo
                    Exit
                    End If
                    Next
                End If
      
        End If
        End If
      
        If n Is CheckBox Then

             'here is my problem...
          
        End If
      

    Next
  
    Loop
  
    cur.close  
  
End Sub




Sub getfields As String
  
    Dim su As StringBuilder
    su.Initialize
  
    su.Append("SET ")
    Dim o As Int

    For Each n As Node In Pane1.GetAllViewsRecursive
      
        Dim jo As JavaObject
        jo=n
      
        If n Is TextField Then
            If n.Tag<>"" Then
            If jo.RunMethod("getText", Null)<>"" Then
                o=o+1
                If o>1 Then
                    su.Append(",")
                End If
            su.Append(n.tag & "=" )
            If CSSUtils.GetStyleProperty(n,"-fx-alignment")="center-right" Then
                su.Append(jo.RunMethod("getText", Null))
            Else
                su.Append("'" & jo.RunMethod("getText", Null)&"'")
            End If
            End If
            End If
        End If
        If n Is DatePicker Then
            If n.Tag<>"" Then
            If jo.RunMethod("getValue", Null)<>"" Then
                o=o+1
                If o>1 Then
                    su.Append(",")
                End If
                su.Append(n.tag & "='" & jo.RunMethod("getValue", Null) & "'" )
            End If              
            End If
        End If
        If n Is CheckBox Then
            If n.Tag<>"" Then

                If jo.RunMethod("Isselected", Null)=True Then
                    o=o+1
                    If o>1 Then
                        su.Append(",")
                    End If
                    su.Append(n.tag & "=1" )
                Else
                    o=o+1
                    If o>1 Then
                        su.Append(",")
                    End If
                    su.Append(n.tag & "=0" )
                End If
            End If
        End If
      
    Next
  
    Dim s As String
    s=su.ToString
    Log(s)
    Return s
  
End Sub

Sub cancelit_Action
    frm1.close  
End Sub



So that works like this:

From Main Form when i want to add new data at my db... call this:

B4X:
Sub newpel_action
    formtitle="New Customer"
    formjob="NEWPEL"
    Dim mf As newform
    mf.Initialize(MainForm)
    Dim s As String
    s = mf.Show
End Sub

That code help me...

not designing every time layout for data inserting forms...

The only i need is to set the fields.. of db with createtextfield, createcheckbox, createdatefield routines

By the way "Isselected" not that i want :-(
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
@Erel

At my code there is...

at sub readfields:
B4X:
    For Each n As Node In Pane1.GetAllViewsRecursive
       
        Dim jo As JavaObject
        jo=n
....

I am talking for my code... do i need to change something - or it's ok ... do you think is better to do something else ?
 
Upvote 0
Top