Here is the code that I used to make a tabular UI.

DynamicForm[row_, col_] :=DynamicModule[{x = ConstantArray[0, {row, col}], a}, 
    a = {Column[(Row[#] & /@Table[With[{i = i, j = j}, 
          InputField[Dynamic[x[[i, j]]], Number,FieldSize -> Tiny]
          ], {i, 1, row}, {j, 1, col}])]};
    AppendTo[a, {CancelButton[], DefaultButton[DialogReturn[]]}];
    CreateDialog[Column[Flatten@{a}, Spacings -> {1, Automatic},Alignment -> Left],
    Modal -> True];

enter image description here

How can I retrieve the user input form the dialog?


2 Answers 2


Assign your input in DialogReturn, e.g. to a variable return. Apart from from this, and omitting your Dynamic@x at the end, your code is unchanged:

DynamicForm[row_, col_] := 
 DynamicModule[{x = ConstantArray[0, {row, col}], a}, 
  a = {Column[(Row[#] & /@ 
       Table[With[{i = i, j = j}, 
         InputField[Dynamic[x[[i, j]]], Number, 
          FieldSize -> Tiny]], {i, 1, row}, {j, 1, col}])]};
   a, {CancelButton[], DefaultButton[DialogReturn[return = x]]}];
   Column[Flatten@{a}, Spacings -> {1, Automatic}, Alignment -> Left],
    Modal -> True]]

Now run the dialog and enter your data:

DynamicForm[2, 3];

enter image description here

You can obtain the input from return:


{{1, 2, 3}, {4, 5, 6}}


If you want the evaluation to halt until the Dialog returns you can get your code working very simply:

 DynamicForm[row_, col_] :=
 DialogInput[{x = ConstantArray[0, {row, col}], a}, 
  Column[Flatten@{{Column[(Row[#] & /@ 
     Table[With[{i = i, j = j}, 
       InputField[Dynamic[x[[i, j]]], Number, 
        FieldSize -> Tiny]], {i, 1, row}, {j, 1, col}])],
     {CancelButton[], DefaultButton[DialogReturn[x]]}}}, 
  Spacings -> {1, Automatic}, Alignment -> Left]]

Note that it's the function DialogInput[] (used here instead of CreateDialog[]) that halts the evaluation, see How to evaluate following inputs only after finishing CreateDialog

This allows you to just use DynamicForm in place of it's input in equations and such for instance: a=Transpose[DynamicForm[23,34]] will open a popup and assing a to the tranposed result upon pressing the OK button.


If you don't want the dialog to return it's value, here is a version that puts the changes into a dynamically updated variable:

setDyn[a_Dynamic, val_] := Extract[a, 1, Function[{w}, w = val, HoldAll]];

DynamicForm[a_Dynamic, row_: 2, col_: 2] :=
 DynamicModule[{x = ConstantArray[0, {row, col}]},
 setDyn[a, x];
 Column[{Grid@#1, Row@#2}, Spacings -> {1, Automatic}, Alignment -> Center]
     & @@ {
      With[{i = i, j = j}, 
        Dynamic[x[[i, j]], 
         Function[{val, nam}, nam = val; setDyn[a, x], HoldRest]], 
        Number, FieldSize -> Tiny]],
     {i, row}, {j, col}],
    {DefaultButton["Close", DialogReturn[]]}

Called with DynamicForm[Dynamic[a],4,4] it will update a as you change it.

  • $\begingroup$ Your answer was great too specially I liked the use of setDyn. $\endgroup$ Commented Dec 20, 2012 at 9:13
  • $\begingroup$ Very good answer. Two problem: (1) when DialogInput is created, the first InputField doesn't get the focus: it would be great if it got, because now a click is required to write in the field. (2) when the last InputFiled has the focus, TAB cycles the focus to the first one. That's a bit confusing because, again, a click is necessary, instead of the usual TAB key, to pass the focus to the buttons. $\endgroup$ Commented May 4, 2016 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.