Skip to main content

gui construction - Problems with visual update in GUI


Consider the following. When clicking in "Browse" and then choosing a file, the path will remain the original one and won't change to the one of the file I chose:


nb = EvaluationNotebook[];

DynamicModule[
{path1 = FileNameJoin[{$HomeDirectory, "Desktop", "file.nb"}],
path2 = FileNameJoin[{$HomeDirectory, "Desktop", "file.xls"}]},

CreateDialog[
Column[{Row[{InputField[Dynamic[path1], String, Enabled -> False],
FileNameSetter[Dynamic[path1],
"Open", {"Mathematica Notebook" -> {"*.nb"}}]}],
Row[{InputField[Dynamic[path2], String, Enabled -> False],
FileNameSetter[Dynamic[path2],
"Open", {"Excel files" -> {"*.xls", "*.xlsx"}}]}]}],
NotebookEventActions :> {"WindowClose" :> (SetOptions[nb,
Visible -> True])}]
]


Answer



The behaviour is indeed inconsistent: CreateDialog uses the option WindowSize -> All, where All (god knows why) causes the unexpected behaviour with dynamic content. See resolution of the problem at the end of the post.


The following even more simple example clearly shows that something fishy is going on: only path2 can be set via the second button but not path1:


DynamicModule[{path1 = "path #1", path2 = "path #2"},
CreateDialog@Grid@{
{FileNameSetter[Dynamic@path1,
"Open", {"Mathematica Notebook" -> {"*.nb"}}], Dynamic@path1},
{FileNameSetter[Dynamic@path2,
"Open", {"Excel files" -> {"*.xls", "*.xlsx"}}], Dynamic@path2}
}];


Removing the CreateDialog wrapper causes both FileNameSetters to work as expected, which indicates that it is the special context of the dialog window that causes the erroneous behaviour. Since CreateDialog[expr] is just a specialized CreateWindow[DocumentNotebook[expr]], we have to look at the options CreateDialog uses as defaults for its internal CreateWindow. As it turns out, the blame lies with WindowSize. Compare the behavior of the following two examples where only the value of WindowSize differs.


This is faulty:


DynamicModule[{path1 = "path #1", path2 = "path #2"},
CreateWindow[DocumentNotebook@Grid@{
{FileNameSetter[Dynamic@path1,
"Open", {"Mathematica Notebook" -> {"*.nb"}}], Dynamic@path1},
{FileNameSetter[Dynamic@path2,
"Open", {"Excel files" -> {"*.xls", "*.xlsx"}}], Dynamic@path2}
}, WindowSize -> All]]


This works as expected:


DynamicModule[{path1 = "path #1", path2 = "path #2"},
CreateWindow[DocumentNotebook@Grid@{
{FileNameSetter[Dynamic@path1,
"Open", {"Mathematica Notebook" -> {"*.nb"}}], Dynamic@path1},
{FileNameSetter[Dynamic@path2,
"Open", {"Excel files" -> {"*.xls", "*.xlsx"}}], Dynamic@path2}
}, WindowSize -> {200, 200}]]


And then I remembered: Strange behavior of CreateDialog in (Dynamic)Module


The options for CreateWindow (to make it look like it is a dialog) are:


{WindowSize -> {200, 200}, Deployed -> True, ShowCellBracket -> False,
WindowFloating -> False, StyleDefinitions -> "Dialog.nb",
WindowFrame -> "ModelessDialog"}

Comments