Java Question Call a Java function when user close B4J

stevel05

Expert
Licensed User
Longtime User
I imported it now but serves just for this issue and consume memory.
Yes, it is probably not worth the overhead to provide a convenience function.
 

max123

Well-Known Member
Licensed User
Longtime User
So I've tried this, no errors in Eclipse, but it is preferrable pass the Form from B4J as Object or as Pane ?
I've added a cast, but probably is wrong...
Java:
    public void Initialize(BA parent, Object form, String EventName, int in_device_num, int out_device_num) {
         this.parent = parent;
         this.eventName = (EventName.toLowerCase(BA.cul));
     
         Init(parent, null);
         AddInput(in_device_num);
         AddOutput(out_device_num);
     
         setCloseListener((Pane)form);
    }
 
    public void setCloseListener(Pane node) {
         Stage stage = (Stage)node.getScene().getWindow();
         stage.setOnCloseRequest((EventHandler<WindowEvent>) new EventHandler<WindowEvent>() {
                     
         public void handle(WindowEvent e) {
           BA.Log("--- Close All Devices ---");
           CloseAllDevices();
         }
     });
    }
 

stevel05

Expert
Licensed User
Longtime User
On my way out now, will have a look tomorrow if you haven't sorted it.
 

max123

Well-Known Member
Licensed User
Longtime User
Ok, thanks Steve for your great help.
This is my last, should accept MainForm.RootPane, I will try it and tomorrow inform you.
Java:
    public void Initialize(BA parent, Pane p, String EventName, int in_device_num, int out_device_num) {
         this.parent = parent;
         this.eventName = (EventName.toLowerCase(BA.cul));
    
         Init(parent, null);
         AddInput(in_device_num);
         AddOutput(out_device_num);
    
         setCloseListener(p);
    }
 
    public void setCloseListener(Pane node) {
         Stage stage = (Stage)node.getScene().getWindow();
         stage.setOnCloseRequest((EventHandler<WindowEvent>) new EventHandler<WindowEvent>() {
                    
         public void handle(WindowEvent e) {
           BA.Log("--- Close All Devices ---");
           CloseAllDevices();
         }
     });
    }
Thanks, good evening. šŸ‘Œ
 

max123

Well-Known Member
Licensed User
Longtime User
Hi Steve, sorry for delay on reply but I had not time to work with the project.

Today I've tried it and this worked, the java code is as last post, on B4J I Initialize this way:
B4X:
Midi.Initialize2(MainForm.RootPane, "Midi", -1, -1, "myMidiBus") ' Initialize one midi bus whithout open input and open devices. Assign an event callback and a bus name just for reference.

By commenting the Midi.Close on MainForm_CloseRequest and MainForm_Closed
B4X:
Sub MainForm_Closed
    Log(">>>>>> MainForm_Closed")
    Log("BYE")
End Sub

Sub MainForm_CloseRequest (EventData As Event)
'    Midi.Close
    Log(">>>>>> MainForm_CloseRequest")
End Sub
.... when I close the B4J app with X on top-right of form the java function is correctly called and automatically closes all opened midi in and out port if any, this is the log I receive:
Form focus: true
--- Close All Devices ---
MidiBus: Closing All Midi Devices
MidiBus: Done
Form focus: false
>>>>>> MainForm_Closed
BYE
..... but as you said it completely do not call MainForm_CloseRequest, this is a problem because if here there are other code to close other stuffs this is not called at all.

Now I will try it, maybe when MainForm_Close fires I can put a call inside it to call MainForm_CloseRequest to just execute the code on the end. I'm not sure if it works.... I will try it now... but is not too good way to work with....

Another issue is that I placed on the form an EXIT button, when pressed it just call ExitApplication, when this is called the application exit without call a java function, so midi ports are not closed.

Here need to know what really B4X do, seem the ports already closed when the class instance dead, personally I never had problems to open midi ports not well closed, I had this type of problems with sockets but not midi, probably it already automatically closes all ports.
 
Last edited:
Top