B4J Question Count Up/Count Down

db0070

Active Member
Licensed User
Longtime User
I am trying to make a simple text box with up/down attached to it, so if the user presses 'up' the text box increments by one, and if the user presses 'down' it decrements by one.

I understand I could do this using two extra buttons, one controlling up and one controlling down. Is there a simpler way?
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
You can use the MouseClicked() event and increment if the left mouse button was clicked and decrement if the right mouse button was clicked.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could use the spinner control.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Example TextField as Spinner Control using a JavaObject - not using ControlsFX.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Spinner.html

SpinnerTextField defined as JavaObject:
B4X:
Private joSpinner As JavaObject

B4X:
'Add a SpinnerTextField to the RootPane
Sub AddSpinner
   Dim spMin As Double = 0
   Dim spMax As Double = 10
   Dim spInitialValue As Double = 5
   Dim spAmountToStepBy As Double = 1
   joSpinner.InitializeNewInstance("javafx.scene.control.Spinner", Array(spMin, spMax, spInitialValue, spAmountToStepBy))
   MainForm.RootPane.AddNode(joSpinner, 20, 100, 100, 20)
End Sub

B4X:
'Get the value of the SpinnerTextField
Sub GetSpinnerValue As Double
   Return joSpinner.RunMethodJO("getValueFactory", Null).RunMethod("getValue", Null)
End Sub

B4X:
'Set the value of the SpinnerTextField
Sub SetSpinnerValue(value As Double)
   joSpinner.RunMethodJO("getValueFactory", Null).RunMethod("setValue", Array(value))
End Sub
 
Last edited:
Upvote 0

db0070

Active Member
Licensed User
Longtime User
Thanks for that code, I will try it out later. If I understand this correctly, I will need a second spinner for the down count.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
No need for a second spinner as the spinner handles by default both directions.

Pls find project attached.
 

Attachments

  • B4JHowToTextFieldSpinner.zip
    2 KB · Views: 210
Upvote 0

db0070

Active Member
Licensed User
Longtime User
I could not use the code as is, as I am targeting Android or iPhone. Anyway, I tried to modify the code using B4A, and it fails at this line

joSpinner.InitializeNewInstance("javafx.scene.control.Spinner", Array(spMin, spMax, spInitialValue, spAmountToStepBy))

I have added the JavaObject library.
 
Upvote 0
Top