B4J Question Minimize on Tray when close

Marcos Alves

Well-Known Member
Licensed User
Hello All,

is there any way in B4J to change the "x" behavior when closing a window (B4XPages) to, instead of to close the program, to send the app to system tray/background?

Thanks!
 

stevel05

Expert
Licensed User
Something like this maybe:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI 
    Private Button1 As B4XView
    Private MoveY As Double
    Private Duration As Double = 1000
    Private TimerInterval As Long = 15
    
    Private InitialY, FinalY As Double
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show

    Dim Screen1 As Screen = fx.PrimaryScreen
    FinalY = Screen1.MaxX + 10
    
End Sub

Private Sub MainForm_CloseRequest (EventData As Event)
    EventData.Consume
    InitialY = MainForm.WindowTop
    MoveY = (FinalY - InitialY) / (Duration / TimerInterval)
    Do While MainForm.WindowTop < FinalY
        MainForm.WindowTop = MainForm.WindowTop + MoveY
        Sleep(TimerInterval)
    Loop
    MainForm.close
End Sub
 

Attachments

Last edited:

stevel05

Expert
Licensed User
Sorry, in my haste I mixed up the axes, changed them now, but I'm sure you can get what you want from it.
 

Marcos Alves

Well-Known Member
Licensed User
I read it as the animation, I'm sure Marcos will let us know.
Thanks @stevel05 and @Erel .

@Erel is correct. @stevel05 Yesterday I read your answer in my mobile phone and hadn't chance to test up to now. The solution presented by @stevel05 does some of the behaviors those I need, but not all:
- When x is clicked, the program isn't stopped because the event is consumed (EventData.Consume - great! I didn't know this resource in B4J) BUT
- The window is minimized (not hidden) and I still need the system tray icon

Remember also that I'm using B4XViews which could request some modification in the commands... (_CloseRequest (EventData As Event) - what's the "form" name in B4XViews program?)... must to check...

Then, I think that the complete solution will be using jSystemTray like @Erel mentioned (which probably will consume the "close" event also as @stevel05 used in his code). I'll check jSystemTray, test and update here! Thanks !!!
 

stevel05

Expert
Licensed User
Try the jSystemTray on it's own first and see how it works in the example. If you want the added animation, then add it afterwards.

There is no equivalent B4xView for a Form as it is not a view, just use the Form and it's events.
 

Marcos Alves

Well-Known Member
Licensed User
Try the jSystemTray on it's own first and see how it works in the example. If you want the added animation, then add it afterwards.

There is no equivalent B4xView for a Form as it is not a view, just use the Form and it's events.
But I have a doubt... in a B4XPage class called, let's say "orders"... what's the form name to capture the events?
 

stevel05

Expert
Licensed User
The events for a form will only be raised in the Module in which the form is created. If you haven't created any additional forms, then the code will need to go in the main module. The events will be called on the Mainform.
 
Last edited:

Marcos Alves

Well-Known Member
Licensed User
The events for a form will only be raised in the Module in which the form is created. If you haven't created any additional forms, then the code for the systemtray will need to go in the main module. The events will be called on the Mainform.
Ok, which means the module in B4XPages which has: B4XPages.AddPage or B4XPages.ShowPage - understood ... To use B4XPages looks a little bit more difficult than only programming in B4J classic but I hope that the B4A and B4i codes automatically generated will worth! Let's see.
 

stevel05

Expert
Licensed User
The System tray is not a cross platform feature, it is only relevant to B4j.

In the past I often used classes to manage different aspects of the Gui with B4j and B4a to avoid code getting unmanageable in one module, B4xPages handles this kind of structure in a consistent manner which always makes life easier.
 

Marcos Alves

Well-Known Member
Licensed User
The System tray is not a cross platform feature, it is only relevant to B4j.

In the past I often used classes to manage different aspects of the Gui with B4j and B4a to avoid code getting unmanageable in one module, B4xPages handles this kind of structure in a consistent manner which always makes life easier.
Yes! Many aspects of a complex app must to be managed for each platform. This one that I'm coding, for example. In B4A I use Firebase Cloud Messages to wake up the app and manage the server requests. In Windows (B4J), I must to maintain a socket open (complex at server side) or to make periodic samples for server requests (highly demanding for the server when the client number increases ). And in IOS I know that the best solution will be to implement the IOS Cloud Messages...
But I'm trying to use the same code always when it's possible - this is my first try in B4XPages - with the valuable help of you @stevel05 , @Erel and the great B4X community I'm sure that I'll be successful !
Thanks!
 

Erel

Administrator
Staff member
Licensed User
The events for a form will only be raised in the Module in which the form is created. If you haven't created any additional forms, then the code will need to go in the main module. The events will be called on the Mainform.
Note that in B4J, each B4XPage is a separate form. You can get the form with B4XPages.GetNativeParent(Me).

Remember also that I'm using B4XViews which could request some modification in the commands... (_CloseRequest (EventData As Event) - what's the "form" name in B4XViews program?)... must to check...
It is very simple. See the tutorial (post #7): https://www.b4x.com/android/forum/t...k-for-managing-multiple-pages.118901/#content
 

Marcos Alves

Well-Known Member
Licensed User
Note that in B4J, each B4XPage is a separate form. You can get the form with B4XPages.GetNativeParent(Me).


It is very simple. See the tutorial (post #7): https://www.b4x.com/android/forum/t...k-for-managing-multiple-pages.118901/#content
Thanks @Erel . I'm learning and adapting the code for B4XPages and need to confess that I'm curious to see and test the the generated B4i/B4A result and discover how much I'll need to work on the generated multiplatform code. It looks that will save a lot of coding! Thanks for this great job!
 
Top