Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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"?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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!

share|improve this answer
    
I don't at first see why this works with Tab, but it does, and in v7 as you say. +1 :-) –  Mr.Wizard Sep 5 '13 at 14:51
    
I see now! It was my use of Row that was messing things up. –  Mr.Wizard Sep 5 '13 at 14:53
1  
@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... –  Albert Retey Sep 6 '13 at 14:54
    
@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... –  Albert Retey Sep 6 '13 at 15:42

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, #]] &]]
share|improve this answer
    
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! –  Gabriel Sep 5 '13 at 14:27
1  
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! –  Gabriel Sep 5 '13 at 14:35
    
@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! –  Mr.Wizard Sep 5 '13 at 14:42
    
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! –  Gabriel Sep 5 '13 at 14:47
1  
@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. –  ybeltukov Sep 5 '13 at 14:52

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

share|improve this answer
    
This apparently doesn't work in v7, even with out FieldHint -> "Enter Number". –  Mr.Wizard Sep 5 '13 at 14:14
    
@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 :) –  PlatoManiac Sep 5 '13 at 14:16
    
@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? –  Albert Retey Sep 6 '13 at 14:55
    
@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... –  Albert Retey Sep 6 '13 at 15:06
    
@AlbertRetey You are absolutely right about the Block. I missed it somehow. I updated that part. Thx! –  PlatoManiac Sep 6 '13 at 15:19

Your Answer

 
discard

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.