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:
Last edited:
Upvote 0
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
You could use the spinner control.
 
Upvote 0
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:
Code: Copy
Private joSpinner As JavaObject

Code: Copy
'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

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

Code: Copy
'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
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
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
Back
Top