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.

@pinyazhin Dear Roman, could you please re-write the code for Jmix2.x? I am lost and cant get it working.
Especially the shortcuts ant the Actions are a nightmare.
Thank you very much in advance.
Regards
Tomas

Hello Tomáš!

This is the same code in Jmix 2.x:

dialogs.createInputDialog(this)
                .withParameter(InputParameter.stringParameter("name"))
                .withActions(
                        new InputDialogAction("ok")
                                .withText("OK")
                                .withVariant(ActionVariant.PRIMARY)
                                .withShortcutCombination(KeyCombination.create("CONTROL-ENTER"))
                                .withHandler(actionPerformedEvent -> {
                                    InputDialogAction dialogAction = (InputDialogAction) actionPerformedEvent.getSource();
                                    InputDialog inputDialog = dialogAction.getInputDialog();

                                    String value = inputDialog.getValue("name");
                                    // process the value
                                    inputDialog.close(InputDialog.INPUT_DIALOG_OK_ACTION);
                                }),
                        new InputDialogAction("cancel")
                                .withText("Cancel")
                                .withHandler(actionPerformedEvent -> {
                                    InputDialogAction dialogAction = (InputDialogAction) actionPerformedEvent.getSource();
                                    dialogAction.getInputDialog().close(InputDialog.INPUT_DIALOG_CANCEL_ACTION);
                                }))
                .open();

Note that shortcuts for InputDialogAction does not work correctly, so I created an issue: Shortcut for InputDialogAction does not work · Issue #3617 · jmix-framework/jmix · GitHub

You can find examples of InputDialog here: UI Samples :: input-dialog
And documentation: Dialogs :: Jmix Documentation