B4J Question How to set the value of a progressbar?

positrom2

Active Member
Licensed User
Longtime User
pb1 is refers to a progressbar created in the SceneBuilder.
B4X:
Private pb1 As Node
Private progress As JavaObject
progress=pb1
progress.InitializeNewInstance("javafx.scene.control.ProgressBar",Null)
progress.RunMethod("setProgress",xvalue)
"setProgress" is not known.
 

stevel05

Expert
Licensed User
Longtime User
You need to wrap the passed values in an array:

B4X:
progress.RunMethod("setProgress",Array As Object(xvalue)

And if pb1 is already a progress bar, you just need to do:

B4X:
Dim progress As JavaObject  =  pb1
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
i've just tried it out, this works:

B4X:
    Dim P As JavaObject = pb1
Dim prog As Double = .5
P.RunMethod("setProgress",Array As Object(prog))
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Thanks, it works, indeed. Little strange is that prog needs to be a Double, a xvalue as Byte for instance has to be converted as prog=xvalue.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Based on what has been shared have extended with getProgressBarValue. Usage of JavaObjects is so powerful. Thanks to Steve.

B4X:
' Set the value of a progressbar
' Parameter ProgressBar as node
' Parameter Value between 0 and 1 to set as double
' Example:  setProgressBarValue(progressbarMain, 0.90)
Sub setProgressBarValue(pb As Node, value As Double)
  If (value < 0) OR (value > 1) Then Return
  Dim P As JavaObject = pb
  Dim progress As Double = value
  P.RunMethod("setProgress",Array As Object(progress))
End Sub

' Get the value of a progressbar
' Parameter ProgressBar as node
' Returns Value between 0 and 1 as double
' Example: "Progressbar: " & (getProgressBarValue(progressbarMain) * 100) & "%"
Sub getProgressBarValue(pb As Node) As Double
  Dim P As JavaObject = pb
  Return P.RunMethod("getProgress",Null)
End Sub
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks, that extension is good. Probably also the colors could be set (preferably In-Line?)
Use ProgressBar.Style Property. Example red bar with black border:
B4X:
progressbar.style="-fx-box-border: black; -fx-accent: red;"
 
Upvote 0
Top