B4J Question Add a Spinner Control via code

DMW

Member
Licensed User
Longtime User
Hi all,

It can easily be very confusing when trying to add a Spinner Control... At least it is for me...

The following solution (Thanks to Robert Linn's excellent site) works great:

B4X:
'SpinnerTextField defined as JavaObject:
Private joSpinner As JavaObject

'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

However, in my project I have 1 TabPane with 4 TabPages. I'm trying to make the Spinner Control visible only in TabPage 2.

How can I achieve that?

Added:
The following solution does not report any error but on the other hand the Spinner Control is not visible:
B4X:
Sub Add_Spinner
   
      Dim spMin As Double = 1
      Dim spMax As Double = 500
      Dim spInitialValue As Double = 1
      Dim spAmountToStepBy As Double = 1
      joSpinner.InitializeNewInstance("javafx.scene.control.Spinner", Array(spMin, spMax, spInitialValue, spAmountToStepBy))
   
    Dim Spinner As Pane
    Spinner.Initialize("Spinner")
    Spinner.RemoveNodeFromParent
    Spinner.AddNode(joSpinner, 130, 320, 60, 30)
   
End Sub

TIA,
Dennis
 
Last edited:

DMW

Member
Licensed User
Longtime User
Thanks for the tip. However even if the code compile fine the Spinner control is not visible at all.
I have tried all the findings (in this forum) to no avail.
Must I add the pane via code as well?

Here is the main Sub:
B4X:
Sub Add_Spinner
   
    Dim Spinner As Pane
   
      Dim spMin As Double = 1
      Dim spMax As Double = 500
      Dim spInitialValue As Double = 1
      Dim spAmountToStepBy As Double = 1
      joSpinner.InitializeNewInstance("javafx.scene.control.Spinner", Array(spMin, spMax, spInitialValue, spAmountToStepBy))
       
    Spinner.Initialize("Spinner")
    Spinner.RemoveNodeFromParent
    Spinner.AddNode(joSpinner, 98, 51, 60, 30)
   
    Log(joSpinner.RunMethodJO("getValueFactory", Null).RunMethod("getValue", Null))
           
End Sub
The log shows it exist as it gives the present value

TIA,
Dennis
 
Last edited:
Upvote 0

DMW

Member
Licensed User
Longtime User
Please bear with me...

I have the following code where the main layout file is loaded in the below Sub:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("licmanlayout") 'Load the layout file.

    Add_Spinner
    Initialize_TabPane
                        
    MainForm.Title="License Manager"
    MainForm.Show
    
End Sub

The following Sub add the Spinner Control to the pSpinner Pane:
B4X:
Sub Add_Spinner

    Dim pSpinner As Pane

      Dim spMin As Double = 1
      Dim spMax As Double = 500
      Dim spInitialValue As Double = 1
      Dim spAmountToStepBy As Double = 1
  
    joSpinner.InitializeNewInstance("javafx.scene.control.Spinner", Array(spMin, spMax, spInitialValue, spAmountToStepBy))
    
    pSpinner.Initialize("pSpinner")
    pSpinner.RemoveNodeFromParent
    pSpinner.AddNode(joSpinner, 200, 320, 100, 30)

End Sub

The following Sub load the layout files for the 4 Tabpages.
The pSpinner Pane (with the Spinner Control) is part of the tab2 layout file.
B4X:
Sub Initialize_TabPane()
  
    Dim tp1 As TabPage = TabPane1.LoadLayout("tab1","Registered License")
    tp1.Image = fx.LoadImageSample(File.DirAssets, "17_g.png", 24, 24)
    Dim tp2 As TabPage = TabPane1.LoadLayout("tab2","Add/Edit/Delete Licenses")
    tp2.Image = fx.LoadImageSample(File.DirAssets, "31_g.png", 24, 24)
    Dim tp3 As TabPage = TabPane1.LoadLayout("tab3","Settings")
    tp3.Image = fx.LoadImageSample(File.DirAssets, "39_g.png", 24, 24)
    Dim tp4 As TabPage = TabPane1.LoadLayout("tab4","About")
    tp4.Image = fx.LoadImageSample(File.DirAssets, "20_g.png", 24, 24)
      
End Sub

Apparently it's something very basic I miss here. Please guide me.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

this code snippet might help. It adds a spinner object (view) to the anchorpane of tabpage2.
B4X:
Sub Process_Globals
    Private tpMain As TabPane
    Private tp1 As TabPage
    Private tp2 As TabPage
    Private tp3 As TabPage
...

Sub AppStart (Form1 As Form, Args() As String)
...
    tp1 = addTabPage("Tab A")
    tp2 = addTabPage("Tab B")
    addSpinner(tp2.Content)
    tp3 = addTabPage("Tab C")
...

Sub addTabPage(text As String) As TabPage
    Dim tabpageNew As TabPage
    tabpageNew.Initialize
    tabpageNew.Id = "tabpage" & (tpMain.Tabs.Size + 1)
    tabpageNew.Text = text
    Dim ap As AnchorPane
    ap.Initialize("")
    tabpageNew.Content = ap
    tpMain.Tabs.Add(tabpageNew)
    Return tabpageNew
End Sub

Sub addSpinner(p As Pane)
    Private joSpinner As JavaObject
   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))
   p.AddNode(joSpinner, 20, 100, 100, 20)
End Sub

Output
upload_2016-9-27_17-56-46.png
 
Upvote 0

DMW

Member
Licensed User
Longtime User
Thank You for all the help!

The solution to it was this line:
B4X:
addSpinner(tp2.Content)

By implementing it I managed to solve the issue :)

I also notice that we, as usual, can skin a cat in several ways.

The next and final question at this stage is how can I change the Tab order for the Spinner Control?

Thanks in advance,
Dennis
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Tabpages are stored in the TabPane view property Tabs, a List, which means List methods like get, removeat, insertat etc. can be applied.
' Example moving tappage 3 (at index 2) to the first position (at index 0) in the TabPane.Tabs List.
Dim tp As TabPage
tp = tpMain.Tabs.Get(2)
tpMain.Tabs.RemoveAt(2)
tpMain.Tabs.InsertAt(0, tp)

'Made me thought: For future would be nice to have a draggable tab control - nice Inline Java orJavaObject challenge
 
Upvote 0

DMW

Member
Licensed User
Longtime User
Very interesting, thanks!

BTW, is it possible to manage the Controls Tab Order. If possible I would like to change the added Spinner Control's Tab position.

TIA,
Dennis
 
Upvote 0
Top