B4J Code Snippet TabPane Closing Policy TabPages

Have been looking to handle closure and showing of TabPages added to a TabPane.
This snippet shows a solution using the TabPane Closing Policy.
The JavaObject library is used to access API methods for the TabPane.

upload_2018-4-16_9-11-32.png


Example attached.

B4X:
'B4J HowTo - TabPane Closing Policy
'Add a cross to be able to close a tab and handle the selected tab closing event.
'Policies: ALL_TABS, SELECTED_TAB, UNAVAILABLE.
'Test example uses 4 layouts with policy ALL_TABS and a button to show Tab 3 (index 2) again after being closed.
'An array of tabpages is used to handle closure and showing again of the tabs defined.

Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private TabPane1 As TabPane
   Private TabPages(4) As TabPage
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.RootPane.LoadLayout("Main")
   MainForm.Title = "B4J HowTo - TabPane Closing Policy"
   ' Set the tabpane tab closing policy - must be prior loading the tab layouts
   TabPane_SetTabClosingPolicy(TabPane1, "ALL_TABS")
'   TabPane_SetTabClosingPolicy(TabPane1, "SELECTED_TAB")
'   TabPane_SetTabClosingPolicy(TabPane1, "UNAVAILABLE")   'Default
   ' Add the tab layouts - the array is used to set the tabpage id and the tag toindicate if the tab is visible (1) or hidden (0)
   TabPages(0) = TabPane1.LoadLayout("Tab1", "Tab 1")
   TabPages(0).Id = 0
   TabPages(0).Tag = 1
   TabPages(1) = TabPane1.LoadLayout("Tab2", "Tab 2")
   TabPages(1).Id = 1
   TabPages(1).Tag = 1
   TabPages(2) = TabPane1.LoadLayout("Tab3", "Tab 3")
   TabPages(2).Id = 2
   TabPages(2).Tag = 1
   TabPages(3) = TabPane1.LoadLayout("Tab4", "Tab 4")
   TabPages(3).Id = 3
   TabPages(3).Tag = 1
   ' Add the eventhandler for closing a tab to each of the tabpages
   For Each tp As TabPage In TabPane1.Tabs
       Dim joTP As JavaObject = tp
       Dim oTP As Object = joTP.CreateEvent("javafx.event.EventHandler","TabPane_Tools_Close",False)
       joTP.RunMethod("setOnClosed",Array(oTP))
   Next
   MainForm.Show
End Sub

' Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

' Handle changing a tab
Sub TabPane1_TabChanged (SelectedTab As TabPage)
   If SelectedTab.IsInitialized Then Log($"Tab Selected: ${SelectedTab.Text}"$)
End Sub

' Set the closing policy of a tabpane
' Pane - Tabpane
' Policy - ALL_TABS, SELECTED_TAB, UNAVAILABLE
Sub TabPane_SetTabClosingPolicy(Pane As TabPane, Policy As String)
   Dim joTP As JavaObject = Pane
   Dim joTCP As JavaObject
   joTCP.InitializeStatic("javafx.scene.control.TabPane.TabClosingPolicy")
   If Policy.Length = 0 Then Policy = "ALL_TABS"
   joTP.RunMethod("setTabClosingPolicy", Array(joTCP.GetField(Policy.ToUpperCase)))
End Sub

' Handle closing a tab by clicking on the cross
' The id (or the text) can be used to determine which tab is closed.
' Set the tag to 0 again to indicate the tab is hidden
Sub TabPane_Tools_Close_Event(EventName As String,Args() As Object) As Object
   Dim CEventJO As JavaObject = Args(0)
   Dim tp As TabPage = CEventJO.RunMethodJO("getSource",Null)
   TabPages(tp.id).Tag = 0
   Log($"Closed Tab with ID ${tp.id} and text '${tp.Text}' (#Tabs ${TabPane1.Tabs.size})."$) ' Closed Tab with ID 2 and text 'Tab 3' (#Tabs 3).
   Return False
End Sub

' Example showing Tab 3 again.
' The Tab 3 is inserted right from the selected tab, i.e. if tab selected index is 0 then Tab 3 is inserted at 1 etc.
' Set the tag to 1 again to indicate the tab is visible
Sub Button_ShowTab3_Click
   ' Tab is visible = do nothing
   If TabPages(2).Tag = 1 Then Return
   ' Insert the tab again and set the tag to 1
   TabPane1.Tabs.InsertAt(TabPane1.SelectedIndex + 1, TabPages(2))
   TabPages(2).Tag = 1
   Log($"Tab with ID ${TabPages(2).id} and text '${TabPages(2).Text}' inserted (#Tabs ${TabPane1.Tabs.size})."$)
End Sub
 

Attachments

  • B4JHowToTabPaneClosingPolicy.zip
    7.8 KB · Views: 440
Top