4

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Gabriel
  • 1,877
  • 12
  • 22

3 Answers3

5

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, #]] &]]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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 05 '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 05 '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 05 '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 05 '13 at 14:47
  • @Gabriel Please see the documentation links in this answer. – Mr.Wizard Sep 05 '13 at 14:49
  • 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 05 '13 at 14:52
  • Thank you Mr. Wizard for your help! – Gabriel Sep 05 '13 at 14:54
  • @ybeltukov That was a great addition; thank you. – Mr.Wizard Sep 05 '13 at 15:00
3

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

PlatoManiac
  • 14,723
  • 2
  • 42
  • 74
  • This apparently doesn't work in v7, even with out FieldHint -> "Enter Number". – Mr.Wizard Sep 05 '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 05 '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 06 '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 06 '13 at 15:06
  • @AlbertRetey You are absolutely right about the Block. I missed it somehow. I updated that part. Thx! – PlatoManiac Sep 06 '13 at 15:19
  • @PlatoManiac: you're welcome. Another advantage of that approach is that you could also do the reset of the list with a keyboard shortcut by adding another rule to the EventHandler, e.g. "EscapeKeyDown":>(val={};y=Null;). Using "ReturnKeyDown" instead (or together) with the tab key might also make the interface more convenient to use (IMHO the desired behaviour is an abuse of the tab key event, but well...) – Albert Retey Sep 06 '13 at 15:51
3

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!

ybeltukov
  • 43,673
  • 5
  • 108
  • 212
  • I don't at first see why this works with Tab, but it does, and in v7 as you say. +1 :-) – Mr.Wizard Sep 05 '13 at 14:51
  • I see now! It was my use of Row that was messing things up. – Mr.Wizard Sep 05 '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 06 '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 06 '13 at 15:42