B4J Question How to insert a progress bar in b4xtable?

guandjy

Member
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private b4xtable1 As B4XTable
    Private GraphColumn As B4XTableColumn
    Private progress As ProgressBar
End Sub
public Sub zhichuchaxun
        GraphColumn = b4xtable1.AddColumn("总进度", b4xtable1.COLUMN_TYPE_TEXT)
        GraphColumn.Sortable = False
        GraphColumn.Searchable = False
     Dim req As DBRequestManager = CreateRequest
     Dim cmd As DBCommand = CreateCommand("select_chaxunzhichu", Array("001"))

    Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
    If j.Success Then
        req.HandleJobAsync(j, "req")
        Wait For (req) req_Result(res As DBResult)
        req.PrintTable(res)
        Dim data As List
        data.Initialize
        For Each row() As Object In res.Rows   
        ....
        For i = 1 To GraphColumn.CellsLayouts.Size - 1
            Dim p As B4XView = GraphColumn.CellsLayouts.Get(i)
            Dim Base As B4XView = xui.CreatePanel("")
            p.AddView(Base, 2dip, 2dip, GraphColumn.Width - 4dip, b4xtable1.RowHeight - 4dip)
            Base.LoadLayout("progress")
            Base.Tag = progress
        Next
Sub B4XTable1_DataUpdated
    For i = 0 To b4xtable1.VisibleRowIds.Size - 1
        Dim RowId As Long = b4xtable1.VisibleRowIds.Get(i)
        Dim p As B4XView = GraphColumn.CellsLayouts.Get(i + 1)
        Dim pro As ProgressBar = p.GetView(1).Tag 'Base panel is the 2nd view
        
        If RowId > 0 Then
            Dim row As Map = b4xtable1.GetRow(RowId)
            pro.Progress= row.Get("x")
        
        End If
        
    Next
End Sub
 

guandjy

Member
Screenshot_2023-12-25-22-19-23-449-edit_com.android.browser.jpg
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1703515556522.png


Good place to use DDD: https://www.b4x.com/android/forum/threads/b4x-dse-designer-script-extensions.141312/#content
Don't miss:
1. Call to XUI.RegisterDesignerClass
2. DDD.CollectViewsData in designer script
3. Code needed to layout a layout directly to the table panels.

B4X:
Sub B4XTable1_DataUpdated
    For i = 0 To B4XTable1.VisibleRowIds.Size - 1
        Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
        Dim pnl As B4XView = ProgressColumn.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
        Dim progress As B4XView = dd.GetViewByName(pnl, "ProgressBar1")
        If RowId > 0 Then
            Dim row As Map = B4XTable1.GetRow(RowId)
            progress.Progress = row.Get("Percentage")
            progress.Visible = True
        Else
            progress.Visible = False
        End If
    Next
End Sub
 

Attachments

  • Project.zip
    9.9 KB · Views: 61
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I added 3 lines to the B4XTable1_DataUpdated Sub as shown below. It shows the ProgressBar values on the firrst page, but crashes subsequently.
B4X:
Sub B4XTable1_DataUpdated
    For i = 0 To B4XTable1.VisibleRowIds.Size - 1
        Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
        Dim pnl As B4XView = ProgressColumn.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
        Dim progress As B4XView = dd.GetViewByName(pnl, "ProgressBar1")
        If RowId > 0 Then
            Dim row As Map = B4XTable1.GetRow(RowId)
            progress.Progress = row.Get("Percentage")
            
            '3 below lines added by Mahares
            pnl.GetView(0).Text = progress.Progress   'This is ine 61 wwhere it crashes
            Log(pnl.GetView(0).Text)            
            pnl.GetView(0).BringToFront
            
            progress.Visible = True
        Else
            progress.Visible = False
        End If
    Next
End Sub

B4X:
Error occurred on line: 61 (B4XMainPage)
ava.lang.RuntimeException: Type does not match (class javafx.scene.control.ProgressBar)
at anywheresoftware.b4a.objects.B4XViewWrapper.typeDoesNotMatch(B4XViewWrapper.java:595)

1703627141640.png
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Use dd.GetViewByName to get the correct view.
I am not really sure what to use for the cell label. name:
B4X:
Dim xlbl As B4XView = dd.GetViewByName(pnl.GetView(0),"Label")  'not sure what to put here??
            xlbl.Text = progress.Progress   'crash here
            xlbl.BringToFront
It crashes with this error:
B4X:
Error occurred on line: 66 (B4XMainPage)
java.lang.RuntimeException: Object should first be initialized (B4XView).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
@Mahares please start a new thread and upload the project.

Is it possible to change the colors of the progress bar?
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Hi,
I have already tried the mentioned link for changing the color of the progress bar but am getting runtime error:
B4X:
Sub B4XTable1_DataUpdated
    For i = 0 To B4XTable1.VisibleRowIds.Size - 1
        Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
        Dim pnl As B4XView = ProgressColumn.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
        Dim progress As B4XView = dd.GetViewByName(pnl, "ProgressBar1")
        Dim lbl As B4XView = dd.GetViewByName(pnl, "Label1")
        lbl.Visible = RowId > 0
        progress.Visible = RowId > 0
        If RowId > 0 Then
            Dim row As Map = B4XTable1.GetRow(RowId)
            progress.Progress = row.Get("Percentage")
            lbl.Text = NumberFormat(row.Get("Percentage"), 0, 0)
            If progress.Progress<20 Then
                setProgressBarColor(progress,"red")
            Else
                setProgressBarColor(progress,"green")
            End If
        End If
    Next
End Sub
Sub to set color:
Sub setProgressBarColor(pb As ProgressBar, color As String)
    Dim joPB As JavaObject =pb
    joPB.RunMethodJo("lookup", Array(".bar")).RunMethod("setStyle", Array("-fx-background-color: " & color & ";"))
    ' joPB.RunMethodJo("lookup", Array(".bar")).RunMethod("setStyle", Array("-fx-background-color: red;"))
End Sub
Error:
ERROR:
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Error occurred on line: 85 (B4XMainPage)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
    at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:259)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
    at b4j.example.b4xmainpage._setprogressbarcolor(b4xmainpage.java:234)
    at b4j.example.b4xmainpage._b4xtable1_dataupdated(b4xmainpage.java:211)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:513)
    at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:460)
    at b4j.example.b4xtable$ResumableSub_ImplUpdateDataFromQuery.resume(b4xtable.java:3118)
    at b4j.example.b4xtable._implupdatedatafromquery(b4xtable.java:2619)
    at b4j.example.b4xtable._updatedata(b4xtable.java:2119)
    at b4j.example.b4xtable._refresh2(b4xtable.java:896)
    at b4j.example.b4xtable._refreshnow(b4xtable.java:3631)
    at b4j.example.b4xtable$ResumableSub_SetData.resume(b4xtable.java:537)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA$3.run(BA.java:267)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
    at java.lang.Thread.run(Thread.java:748)
 
Upvote 0
Top