File chooser provides a simple mechanism for navigating through the file system.
File chooser works in two alternative modes:
- Upload. Allows users to navigate the file system and choose the required file. The file is uploaded to the model folder. When the file is uploaded, the file chooser’s Action is executed. In the action code you can access the file, e.g. to read the model's input parameters.
- Download. The user specifies the file name. At the model runtime, the file chooser waits for the file with the specified name to appear in the file system. Once the file is available, the file chooser becomes enabled. By clicking the file chooser the user downloads the specified file.
You may take a look at the simple demo model where the File chooser is combined with the Text file element making it possible for the user of the model to choose a file (e.g. with the model parameters) before starting the model.
Demo model: File Chooser For Text Files Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).To add a file chooser
- Drag the File Chooser element from the Controls palette into the graphical editor.
- From the Type group of buttons, choose the type of the file chooser dialog. Choose Upload, if you want to bring up a file chooser for uploading files (typical use case; reading data prior to model start). If you use the file chooser to upload a file, the uploaded file will get into the model folder and will be accessible in read-only mode.
- Otherwise, use Download to allow the user to choose a file for downloading.
- In the Title box, specify the title of the dialog box, that will be opened on clicking the file chooser.
- In the Action field, type Java code that will be executed each time the file is uploaded/downloaded using this file chooser. This code can read data from the file and use them in the model. The relative filename of the currently selected file is available here as value (local variable of type String).
- General
-
Name — The name of the file chooser. It is used to identify and access the control from code.
Ignore — If selected, the file chooser will be excluded from the model.
Visible on upper agent — If selected, the control will be also visible on the upper agent where this agent lives.
Type — The type of the dialog. Choose Upload if you want to bring up a file chooser enabling model users to upload a file to the model folder (and optionally read data from the file). Likewise, use Download for letting users choose a file for downloading.
Title — The title of the dialog box.
File name filters — [Visible if the Type is set to Upload] Here you specify file type filter for the file chooser. The file chooser will show and allow users to upload the files of the specified types only. You specify the filter as a list of file extensions, e.g.: .png, .jpeg, .jpg.
File — [Visible if the Type is set to Download] The full name of the file to be downloaded by this file chooser.
- Action
-
Here you can type the code that will be executed each time the user uploads/downloads a file using this file chooser. The relative file name of the currently selected file is available here as value (local variable of type String). In the action code you can e.g. read data from the uploaded file and use them into the model.
- Appearance
-
Background color — The control specifies the background color for the file chooser. Click inside the control and choose a color from the set of the most used ones, or choose a custom color using the Colors dialog box. Background color is correctly displayed under Windows with Classic appearance. However, certain operating systems may not support file choosers with user-defined background colors.
Text color — The control specifies the color of the text displaying the selected file name inside the file chooser shape. Click inside the control and choose a color from the set of the most used ones, or choose a custom color using the Colors dialog box.
Font — Specifies the font family for the text displaying the selected file name inside the file chooser shape. You can adjust the size of the text in the field to the right (pt.).
Italic — If checked, the name of the selected file displayed inside the file chooser shape will be italicized.
Bold — If checked, the name of the selected file displayed inside the file chooser shape will be emphasized in bold.
- Position and size
-
Level — Level to which this control belongs.
X — X-coordinate of the control’s upper left corner.
Y — Y-coordinate of the control’s upper left corner.
Width — The width of the control (in pixels).
Height — The height of the control (in pixels).
- Advanced
-
Visible — The control’s visibility. The control is visible when the specified expression evaluates to true, otherwise it is not visible.
Replication — Here you specify the number of copies of this control you want to be created. If you leave this field empty, only one such control will be created.
Show name — If selected, the name of the control will be displayed on the presentation diagram.
Icon — If selected, the file chooser will be considered as a part of the agent’s icon.
- Value
-
Function Description String getValue() Returns the currently selected file name or empty string if no file is selected. void setValue(String fileName) Sets the selected filename to given fileName.
Doesn’t execute user action code.
fileName — the file name.void setValue(String fileName, boolean callAction) Sets the selected filename to given fileName.
Executes user action code (if any exists) if callAction parameter is true.
fileName — the file name.
callAction — if true user action code (if any exists) will be executed.void setValueToDefault() Sets the selected filename of the file chooser to what was provided as the default one.
Doesn’t execute user action code. - Enabling or disabling
-
Function Description boolean isEnabled() Tests if the control is enabled or disabled. Returns true if enabled, otherwise false. void setEnabled(boolean yes) Sets the control enabled or disabled.
yes — If true, the control will be enabled, if false — disabled. - Executing the action
-
Function Description void action() Executes the action associated with a file chooser. - Position
-
Function Description double getX() Returns the X coordinate of the control (namely, the X coordinate of its upper left corner). double getY() Returns the Y coordinate of the control (namely, the Y coordinate of its upper left corner). void setX(double x) Sets the X coordinate of the control.
x — the new value of X coordinate.void setY(double y) Sets the Y coordinate of the control.
y — the new value of Y coordinate.void setPos(double x, double y) Sets new coordinates for the control.
x — the new value of x coordinate.
y — the new value of y coordinate. - Size
-
Function Description double getWidth() Returns the width of the control, in pixels. double getHeight() Returns the height of the control, in pixels. void setWidth(double width) Sets the width of the control equal to width. void setHeight(double height) Sets the height of the control equal to height. - Visibility
-
Function Description boolean isVisible() Returns the visibility of the control. void setVisible(boolean v) Sets the visibility of the control.
v — visibility: if true — the control is set to be visible, if false — not visible. - Group
-
Function Description ShapeGroup getGroup() Returns the group containing this control. - Level
-
Function Description Level getLevel() Returns the level, where this control is located.
-
How can we improve this article?
-