Make ENTER submit Inputdialog / execute primary action

Input dialog is often used to collect a single value and do something with it.

It would be cool if we had a way to let user confirm/submit the primary action of the dialog by hitting ENTER and to having to resort to grabbing a mouse and licking the OK button.

Example input dialog

			dialogs.createInputDialog(this)
				.withCaption("Qty")
				.withParameters(
					InputParameter
						.intParameter("qty")
						.withCaption("Qty caption")
						.withDefaultValue(outstandingQty)
						.withRequired(true)
				)
				.withActions(DialogActions.OK) // primary / only action 
				.withCloseListener(closeEvent -> {
					if (closeEvent.closedWith(DialogOutcome.OK)) {
						Integer qty = closeEvent.getValue("qty");
						doSomethingWithInputValue(qty);
					}
				})
				.show();

How to make it listen to enter and execute primary action?

@julius.ferianc
Hello Julius

I believe what you are looking for is Action.Status.PRIMARY and it is described here: Dialogs :: Jmix Documentation

    .withActions(
            new DialogAction(DialogAction.Type.YES, Action.Status.PRIMARY)
                    .withHandler(e -> doSomething()),
            new DialogAction(DialogAction.Type.NO)
    )

Best regards
Chris

Hi Chris, thanks, yes I’ve seen that but I believe this type of .whitActions() is available only for OptionDialogBuilder, not for InputDialogBuilder. Happy to be proven wrong (ideally by demoing an example for input dialog :slight_smile:

Also in input dialog, I need to collect value(s) from input(s) within close listener as demonstrated in my mock example above after dialog is closed by that action.

Hello!

As @chrisbeaham suggests, you can use withActions() method but with InputDialogAction paramters. For instance:

dialogs.createInputDialog(this)
        .withParameter(InputParameter.stringParameter("name"))
        .withActions(InputDialogAction.action("ok")
                        .withPrimary(true)
                        .withCaption("OK")
                        .withShortcut("CTRL-ENTER")
                        .withHandler(okEvent -> {
                            String name = okEvent.getInputDialog().getValue("name");
                            // process the value
                            okEvent.getInputDialog().close(InputDialog.INPUT_DIALOG_OK_ACTION);
                        }),
                new InputDialogAction("cancel")
                        .withCaption("Cancel")
                        .withHandler(cancelEvent ->
                                cancelEvent.getInputDialog().close(InputDialog.INPUT_DIALOG_CANCEL_ACTION))
        )
        .show();

There is already such issue: Provide ability to set action as primary in InputDialog · Issue #386 · jmix-framework/jmix · GitHub

Thank you Roman, this approach works perfectly. Also thanks for pointing out the GutHub issue, I will subscribe. Best, J.