4
$\begingroup$

I'd like to make a dynamic, changeable list, which has elements that are supplied by the user in an InputField.

I want the program to have an InputField and a "reset" button. Every time the user types an element into the InputField and presses TAB, this element is added to the list. The list is reset to the empty list when clicking on the "reset" button.

Is this possible in Mathematica? The reset part seems easy, but the first part seems difficult. Perhaps should I use a second button, "add element"?

$\endgroup$

3 Answers 3

3
$\begingroup$

You can track value changes with Dynamic@Refresh and TrackedSymbols

list = {};

DynamicModule[{x}, Column[{
   InputField[Dynamic[x], String], 
   Dynamic@Refresh[If[x != "", AppendTo[list, x]; x = "";]; "", TrackedSymbols :> {x}]
    Button["Reset", list = {}]
   }]]

Dynamic[list]

value changes

It works with TAB and Enter.

For numbers:

list = {};

DynamicModule[{x}, Column[{
   InputField[Dynamic[x], Number], 
   Dynamic@Refresh[If[x != Null, AppendTo[list, x]; x = Null;]; "", TrackedSymbols :> {x}]
    Button["Reset", list = {}]
   }]]

Dynamic[list]

Edit: It works in V7!

$\endgroup$
4
  • $\begingroup$ I don't at first see why this works with Tab, but it does, and in v7 as you say. +1 :-) $\endgroup$
    – Mr.Wizard
    Commented Sep 5, 2013 at 14:51
  • $\begingroup$ I see now! It was my use of Row that was messing things up. $\endgroup$
    – Mr.Wizard
    Commented Sep 5, 2013 at 14:53
  • 1
    $\begingroup$ @Mr.Wizard: it probably should be mentioned that it is just by accident that this will work with Tab. With Tab the input is submitted and focus is moved to the next "focusable" input element. Only if there is no other such element as here focus will be reset to the same element. If you have e.g. another InputField in the same cell, the focus will move to that. For the exact specification of the OP of course that doesn't matter... $\endgroup$ Commented Sep 6, 2013 at 14:54
  • $\begingroup$ @Mr.Wizard: it seems that an additional Deploy would make your version with Row instead of Grid also work. I'm not sure whether that is intended, version dependent or documented... $\endgroup$ Commented Sep 6, 2013 at 15:42
5
$\begingroup$

Edit: Now works with either Enter or Tab, thanks to ybeltukov.

Now simplified with your own idea to use Null as the Dynamic variable:

list = {};

Dynamic[list]

Grid@{{InputField[Dynamic[Null, AppendTo[list, #] &]], Button["Reset", list = {}]}}

Type expressions, then press Enter, and watch them appear in list (as viewed with Dynamic).

Note that with either form you will get a Null in the list with a blank entry, and this has nothing to do with Null within the code. The documentation says:

For expressions, a blank input field is taken to have value Null. For strings, it is taken to have value "".

If this is a problem you could use:

InputField[Dynamic[Null, If[# =!= Null, AppendTo[list, #]] &]]
$\endgroup$
8
  • $\begingroup$ Thank you for your fast answer! When I enter x into the InputField, list gives Null, even if I delete the part "x = Null" from your code. Why is that? What does this part of the code "x = Null" mean? Thanks! $\endgroup$
    – Gabriel
    Commented Sep 5, 2013 at 14:27
  • 1
    $\begingroup$ Even this, could we change the Dynamic part by this: InputField[Dynamic[Null, AppendTo[list, #] &]] This seems to work on my side, but I didn't know we could use Null as variable of Dynamic. Is that normal behaviour? Thanks! $\endgroup$
    – Gabriel
    Commented Sep 5, 2013 at 14:35
  • $\begingroup$ @Gabriel Since we are using the second parameter of Dynamic I think it is save to use Null as you did. I'm going to add that to my answer. Great idea! $\endgroup$
    – Mr.Wizard
    Commented Sep 5, 2013 at 14:42
  • $\begingroup$ Ok, great! I have read the documentation about the second parameter of Dynamic, but that's not enough helpful for me. Is there any other resource on the net/in books about Dynamic functionality of Mathematica? Thanks! $\endgroup$
    – Gabriel
    Commented Sep 5, 2013 at 14:47
  • 1
    $\begingroup$ @Mr.Wizard Your answer completely covers my. I forget the second parameter of Dynamic. I just want to add that Column insstead of Row will allow TAB. $\endgroup$
    – ybeltukov
    Commented Sep 5, 2013 at 14:52
3
$\begingroup$

This will do the job!

To see the changes as you add number to the list or reset it evaluate Dynamic@val in the notebook. Its output will let you see what is happening with the list val when you are using the following interface.

val = {};
Row[{
  DynamicModule[{y},
   EventHandler[
    InputField[Dynamic@y, Number, ContinuousAction -> True, 
     FieldHint -> "Enter Number"],
    {{"KeyDown", "\t"} :> (val = Append[val, Setting@y]; Clear[y])}]
   ],
  Button["Reset List", val = {}]
  }, Frame -> True, FrameStyle -> Gray]

enter image description here

$\endgroup$
6
  • $\begingroup$ This apparently doesn't work in v7, even with out FieldHint -> "Enter Number". $\endgroup$
    – Mr.Wizard
    Commented Sep 5, 2013 at 14:14
  • $\begingroup$ @Mr.Wizard that is a unexpected limitation then! Have got no access to v7. If you can correct it for v7 feel free to do so :) $\endgroup$ Commented Sep 5, 2013 at 14:16
  • $\begingroup$ @Mr.Wizard: I just tried this with version 7.0.1 and it seems to work alright (without the FieldHint of course). What exactly is not working for you? $\endgroup$ Commented Sep 6, 2013 at 14:55
  • $\begingroup$ @PlatoManiac: your use of Block is to my understanding not doing anything, especially it doesn't really localize y in any relevant way. Most probably you meant to use DynamicModule instead. It would probably also be more conventional to clear the input field with y=Null instead of Clear[y], but that is more a matter of taste. Other than that I like that your solution unlike the others should work independent on what else is contained in the gui, so +1 anyway... $\endgroup$ Commented Sep 6, 2013 at 15:06
  • $\begingroup$ @AlbertRetey You are absolutely right about the Block. I missed it somehow. I updated that part. Thx! $\endgroup$ Commented Sep 6, 2013 at 15:19

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.