Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 9 down vote accepted

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}}

share|improve this answer

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]]

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.

share|improve this answer
Your answer was great too specially I liked the use of setDyn. – PlatoManiac Dec 20 '12 at 9:13
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. – mitochondrial May 4 at 14:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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