B4J Question [SOLVED] B4XDialog - default button

LucaMs

Expert
Licensed User
Longtime User
(Most likely this should be a "wish").

How to set a default button (one of the three) so that its click event fires when Enter is pressed?

Note that I tried:
B4X:
xDialog.GetButton(xui.DialogResponse_Cancel).As(Button).RequestFocus
but it doesn't seem to work.


EDIT - I found a way, to improve (make it general routines):
B4X:
    Dim r As Reflector
    r.Target = xDialog.GetButton(xui.DialogResponse_Cancel).As(Button)
    r.AddEventFilter("DlgBtn", "javafx.scene.input.KeyEvent.KEY_PRESSED")
    r.AddEventFilter("DlgBtn", "javafx.scene.input.KeyEvent.KEY_RELEASED")

B4X:
Sub DlgBtn_Filter (EventData As Event)
    Dim jo As JavaObject = EventData
    Dim Code As String = jo.RunMethod("getCode", Null)
    Dim EventType As String = jo.RunMethod("getEventType", Null)
    Log(Code)
    Log(EventType)
End Sub
 
Last edited:
Solution
For me using jdk1.8.0_271 this works as expected.
B4X:
    Dialog.Title = "Test"
    Dim RS As ResumableSub = Dialog.Show("Test","OK","No","Cancel")
    Dialog.GetButton(xui.DialogResponse_Cancel).As(JavaObject).RunMethod("setCancelButton",Array(True))
    Dialog.GetButton(xui.DialogResponse_Positive).As(JavaObject).RunMethod("setDefaultButton",Array(True))
    Wait For(RS) Complete(Resp As Int)
    Log(Resp)

But java jdk-11.0.1 requires:

B4X:
    Dialog.Title = "Test"
    Dim RS As ResumableSub = Dialog.Show("Test","OK","No","Cancel")
    Dialog.GetButton(xui.DialogResponse_Cancel).As(JavaObject).RunMethod("setCancelButton",Array(True))
    Dialog.GetButton(xui.DialogResponse_Positive).RequestFocus
    Wait For(RS) Complete(Resp As...

stevel05

Expert
Licensed User
Longtime User
For me using jdk1.8.0_271 this works as expected.
B4X:
    Dialog.Title = "Test"
    Dim RS As ResumableSub = Dialog.Show("Test","OK","No","Cancel")
    Dialog.GetButton(xui.DialogResponse_Cancel).As(JavaObject).RunMethod("setCancelButton",Array(True))
    Dialog.GetButton(xui.DialogResponse_Positive).As(JavaObject).RunMethod("setDefaultButton",Array(True))
    Wait For(RS) Complete(Resp As Int)
    Log(Resp)

But java jdk-11.0.1 requires:

B4X:
    Dialog.Title = "Test"
    Dim RS As ResumableSub = Dialog.Show("Test","OK","No","Cancel")
    Dialog.GetButton(xui.DialogResponse_Cancel).As(JavaObject).RunMethod("setCancelButton",Array(True))
    Dialog.GetButton(xui.DialogResponse_Positive).RequestFocus
    Wait For(RS) Complete(Resp As Int)
    Log(Resp)

I have no idea why the two are different.
 
Upvote 0
Solution

stevel05

Expert
Licensed User
Longtime User
I just downloaded (again, probably) java 11 from the link Erel posted on the B4J install page; in the folder I searched for "swing" and it was not found. Is there a file containing that name, in your folder?
I don't get that error running your program under java 11.0.1 and

B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Page1.Initialize
    B4XPages.AddPage("Page1", Page1)
    
    xDialog.Initialize(Root)
    Dim RS As ResumableSub = xDialog.Show("Message", "Yes", "No", "Cancel")
    xDialog.GetButton(xui.DialogResponse_Cancel).As(JavaObject).RunMethod("setCancelButton",Array(True))
    xDialog.GetButton(xui.DialogResponse_Positive).RequestFocus
    Wait For (RS) Complete (Result As Int)
    Log("Result: " & Result)
End Sub

Works as expected.

It might be worth trying to reinstall Java 11.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Are you testing on a Windows PC or Mac or Linux?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
For me using jdk1.8.0_271 this works as expected.
That code of yours works on my PC too! (jdk1.8.0_231)
(I just changed the default button, which in my case must be Cancel)
B4X:
    xDialog.Title = "Test"
    Dim RS As ResumableSub = xDialog.Show("Test","OK","No","Cancel")
    xDialog.GetButton(xui.DialogResponse_Cancel).As(JavaObject).RunMethod("setDefaultButton",Array(True))
    Wait For(RS) Complete(Resp As Int)
    Log(Resp = xui.DialogResponse_Cancel)
 
Upvote 0
Top