B4J Question call a separate Form in a separate Module, connected by a button

rayzrocket

Member
Licensed User
Longtime User
I want my PC software/app (B4J) to look and feel like my Android app.

The multiple 'screens' Android app UI paradigm is typically realized by using the MVC (model, view, controller) architecture, since we want to build our B4J software as we built the B4A app; we want to show a new B4A 'UI window' (while others are in background or disappear) and go back to the original 'UI main window' by hitting "OK" in the current 'UI window'.

I believe some have asked & started threads related to this central theme I mention above,
"Two 'forms' where does the code go?"
"Call new form"
"how do to create two forms connected by a button?
"

This is what I did and I need your help to improve this and point out flaws(ie: variable, processes, and memory management). I've tested it briefly to find it switch back and forth forms/modules nicely.

Main Module:
B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private ConfigureMainBut As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("LayoutMain") 'Load the layout file.
    MainForm.Show
End Sub

Sub ReStartMain
    MainForm.Initialize("MainForm", 600, 400)
    MainForm.RootPane.LoadLayout("LayoutMain") 'Load the layout file.
    MainForm.Show
End Sub

Sub ConfigureMainBut_Action 'Action is used rather than MouseEvent for testing purposes
    CallSub(MainConfig, "MainConfigStart")
    MainForm.Close
End Sub


The 'Other' or 'Separate' Module (in this case I called it "MainConfig"):
B4X:
'Static code module
Sub Process_Globals
    Private fx As JFX
    Private MainConfigForm As Form 'Form is the UI interface screen object 
    Private ButtonOK As Button
End Sub

Public Sub MainConfigStart
    MainConfigForm.Initialize("MainConfigForm", 200, 375)
    MainConfigForm.RootPane.LoadLayout("LayoutMainConfig") 'Load the layout file.
    MainConfigForm.Show
End Sub

Sub ButtonOK_MouseClicked (EventData As MouseEvent) 'MouseEvent is used rather than Action for testing purposes
    CallSub(Main, "ReStartMain")
    MainConfigForm.Close
End Sub
 

GMan

Well-Known Member
Licensed User
Longtime User
I try to close a SETUP-Form, but it throws always an error.

i call it this way:
B4X:
    If mi.Text = "_Einstellungen" Then
        MainForm.RootPane.LoadLayout("SETUP") 'Load the layout file.
    End If

and close it this way with a button:

B4X:
Sub SetupCancel_Action
    SETUP.Close
        'SETUP.Close()
End Sub

Here is the error:
Error occurred on line: 84 (Main) (thats the code SETUP.Close / SETUP.Close() )
java.lang.NullPointerException
...
..
.
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
Dim SETUP As Form

I suppose you have a MainForm (Form) (other than SETUP) so change Mainform to SETUP (name of new form)

B4X:
 If mi.Text = "_Einstellungen"Then SETUP.RootPane.LoadLayout("SETUP") 'Load the layout file

B4X:
 Sub SetupCancel_Action
SETUP.Close'SETUP.Close()
End Sub

Do you have initialize SETUP form? I've coded something like you and form is closing on Button_action
(My initializating code)

B4X:
Sub Process_Globals
Dim LoadForm As Form

Sub AppStart (Form1 As Form, Args() As String)
If File.Exists(WorkspacePath,DBFile) Then

    Else
      
        LoadForm.Initialize("LoadForm", 1024,768)
        LoadForm.SetFormStyle("UNIFIED")
        LoadForm.RootPane.LoadLayout("Ly_Download_newform")
        LoadForm.Title="Loading Data From Shared Folder"
        LoadForm.Show
    End If
 
Upvote 0
Top