B4J Question Close xui.MsgboxAsync or xui.Msgbox2Async with code

Chris2

Active Member
Licensed User
Longtime User
Based on @Erel's code here....

xui.MsgboxAsync & xui.Msgbox2Async don't have a "cancel" method, so trying to use it generates an error: java.lang.RuntimeException: Method: cancel not found in: javafx.scene.control.Alert
Instead, the 'close' or 'hide' methods can be used:
B4X:
Sub Globals
   Dim Dialog As JavaObject
End Sub

Sub OpenMsgbox
    Dialog = xui.Msgbox2Async("Question?", "Title", "Yes", "Cancel", "No", Null)
    CloseAfter5Seconds
    Wait For Msgbox_Result (Result As Int)
    Dialog = Null
    If Result = xui.DialogResponse_POSITIVE Then
        Log("yes")
    Else If Result = xui.DialogResponse_Cancel Then
        Log("cancel")
    Else If Result = xui.DialogResponse_Negative Then
        Log("no")
    End If
End Sub

Sub CloseAfter5Seconds
   Sleep(5000)
   If Dialog.IsInitialized Then
'        Dialog.RunMethod("hide", Null)
        Dialog.RunMethod("close", Null) 
   End If
End Sub

Notes:
1. Both these methods return an xui.DialogResponse_Cancel result.
2. I think that using the information linked below someone who understands JavaObject better than I do might be able to set the result to xui.DialogResponse_Positive or xui.DialogResponse_Negative using the setResult method before closing the dialog. I'd be interested to see the code to do that if anyone wants to post it here.
ref. https://openjfx.io/javadoc/17/javafx.controls/javafx/scene/control/Alert.html
 
Last edited:
Solution
Close After 5 Seconds:
Sub CloseAfter5Seconds
    Sleep(5000)
    Dialog.RunMethod("getDialogPane",Null).As(JavaObject).RunMethod("getScene", Null).As(JavaObject).RunMethod("getWindow", Null).As(JavaObject).RunMethod("close",Null)
    CallSubDelayed2(Me, "Msgbox_Result", xui.DialogResponse_Positive)  'xui.DialogResponse_Cancel ?
End Sub

XbNnX_507

Active Member
Licensed User
Longtime User
Close After 5 Seconds:
Sub CloseAfter5Seconds
    Sleep(5000)
    Dialog.RunMethod("getDialogPane",Null).As(JavaObject).RunMethod("getScene", Null).As(JavaObject).RunMethod("getWindow", Null).As(JavaObject).RunMethod("close",Null)
    CallSubDelayed2(Me, "Msgbox_Result", xui.DialogResponse_Positive)  'xui.DialogResponse_Cancel ?
End Sub
 
Upvote 0
Solution

DarkoT

Active Member
Licensed User
Hi,
maybe you can create your own message box (as Class module using B4XDialog), which will include timer (and close module after x seconds)... If you need example, I can upload...
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
Sorry, there's a mis-understanding here. I wasn't asking for help, just posting something I thought others might find useful - closing xui.MsgboxAsync & xui.Msgbox2Async by code.

The code I posted in #1 works fine for me. The use of sleep was just the example in @Erel's original B4A code.

@XbNnX_507's code allows a specific result to be returned as well, so I'll mark that as the solution.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…