# How can I defer the loading of a PopupView control until it is used?

Background: I have several PopupViews of which the loadtime adds to the startup time of a program. To improve the perceived performance I want to defer the loading of the PopupView to the moment that they are actually used. I thought about this:

  Clear[filelist];
Clear[obj];
filelist = {};
Dynamic@PopupView[filelist, Dynamic[obj, (obj = #;) &],
Button["Edit", filelist = {1, 2, 3}, ImageSize -> 80]]


The idea works but not as expected, the popupview is not displayed correctly until filelist is loaded. I want to correct this or find an alternative way to set this up.

Question: How can I defer the loading of a PopupView control until it is used ?

-
A simple If[filelist==={}, #, PopupView[filelist, ...]]&[Button...] can fix the layout, but you would still need to click Edit twice before the popup appears –  Rojo Jun 25 '12 at 14:11
Clicking twice is too sloppy. Hmmm. –  ndroock1 Jun 25 '12 at 14:28
Let me make sure I understand. You have a PopupView whose list elements take long to execute, and you want them to only execute when you open the PopupView? –  Rojo Jun 25 '12 at 14:30

First, define PopupView with empty reference list and only substitute real reference list ref into the PopupView if it is first clicked. This is done by wrapping the PopupView in an EventHandler. The dynamically displayed list next to the initially empty PopupView shows that before clicking, it is empty, and only gains its value when first clicked.

DynamicModule[{obj = {}, list = {""}, ref = Range@100, firstClick = True},
{
EventHandler[Dynamic@PopupView[list, Dynamic[obj, obj = #; &]],
{"MouseDown" :> (If[firstClick, list = ref]; firstClick = False)},
PassEventsDown -> True
],
Dynamic@list
}
]


I've tried to solve this with the $f_{start}$ argument of Dynamic's second argument list, but it does not work as it requires not just clicking on the empty PopupView but also selecting one value (even if it is Null) to make the assignment inside Dynamic's second argument happen.

-
What can I say? Superb! - Thank you very much. –  ndroock1 Jun 25 '12 at 19:34