A slider is a control that allows a user to graphically select a numeric value within a limited interval by moving the knob.
Slider elements are often used for to change the values of numeric variables and parameters at model runtime.
Use sliders to select an int or double value from the defined range.
Demo model: Slider Linked To Parameter Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).To add a slider
- Drag the Slider element from the Controls palette into the graphical editor.
- Navigate to the Properties view.
- In the Orientation group of buttons, select whether you want your slider to be Horizontal or Vertical.
- You may want to link a slider to a numeric variable or parameter (when the value of the slider changes, the linked parameter or variable immediately gets that value). To learn how to link a slider to a parameter, see Linking a slider to a numeric parameter or variable.
- You can also define any action to be performed each time the slider’s value is changed. Type the code you want to execute in the Action section of the slider’s properties (the current value of the slider is available here as value (local variable of type double)).
- General
-
Name — The name of the slider. It is used to identify and access the control from code.
Show name — If selected, the name of the slider is displayed in the presentation diagram.
Lock — If selected, the slider is locked; it will not respond to any mouse actions during the model development time. To select it, click its icon in the elements view.
Ignore — If selected, the slider is excluded from the model.
Orientation — The orientation of the slider, either Vertical or Horizontal.
Link to — If you want to link the slider to a numeric variable or a parameter (when the value of the slider changes, the linked parameter or variable immediately gets that value), select this check box and enter the name of the variable or parameter that you want to link to the slider.
Minimum value — The minimum value of the slider.
Maximum value — The maximum value of the slider.
Step — The dynamic expression that specifies the increment by which the slider’s knob can move along the interval between the minimum and maximum values. The step can be of either int or double type. The default type is double.
Default value — [Visible if the Link to checkbox is not selected] The dynamic expression that defines the slider’s default value. If this slider is linked to a variable or parameter, it will use that variable or parameter’s default value, or the closest possible value from the slider’s range of available values.
Enabled — The Boolean expression that determines whether the slider is enabled or disabled.
Action — The code to execute when the user changes the slider position. The current value of the slider is available here as value (variable of type double).
- Position and size
-
Level — The level to which this control belongs.
X — The X coordinate of the top left corner of the control.
Y — The Y coordinate of the top left corner of the control.
Width — The width of the control, in pixels.
Height — The height of the control, in pixels.
- Visibility and presentation
-
Visible — The visibility of the control. Can be set to a dynamic expression that makes the control visible when evaluated to true.
Agent presentation — If selected, the control is be included in the presentation of the agent. This means that if the agent hosting this control is embedded in another agent, the control will remain visible in that higher-level agent.
Agent icon — If selected, the control is considered as a part of the agent’s icon.
- Expert
-
Replication — Dynamically specifies the number of copies of this control to create. Leave empty to place a single control.
A slider is often linked to a numeric variable or a parameter (when the value of the slider changes, the linked parameter or variable immediately gets that value).
To link a slider to a parameter
- In the slider’s properties, select the Link to check box and select the parameter or variable from the combo-box to the right:
- The pop-up list displays the names of all parameters and variables of the valid types. To link to a parameter, simply select it from the list.
Another way to link to a parameter is to select the required parameter in the graphical editor.
To link a slider to a parameter by selecting it in the graphical editor
-
Click the target icon next to the Link to property, or
Click on the Link to edit box. In the dialog box that appears, click Select on canvas. - Select the parameter in the graphical editor.
You can set the value of the slider:
- Manually, by dragging its knob to the desired value;
- By changing the value of the linked parameter or variable and passing it as an argument to the slider’s setValue(value, callAction) function.
When using the value of the linked element to set the value of the slider, the new value can be outside of the slider’s step grid, defined by the Step property of the slider, or outside the slider’s range, defined by the slider’s Minimum value and Maximum value. In this case, a warning appears in the Console view, the slider’s knob moves to the closest value that fits the slider’s specified Range and Step, and the linked parameter or variable is set to that value as well.
- Obtaining the current value
-
Function Description double getValue() Returns the current value of the slider. int getIntValue() Returns the current int value of the slider.
This function may only be used in sliders linked to parameters or variables of type int. - Setting new value
-
Function Description void setValue(double value) Sets the value of the slider. If you call this function at runtime and set the new value outside the specified range, the slider value is corrected to the closest possible value within the slider’s range and step grid, and a warning is displayed. Does not execute user action code.
value — the new value.void setValue(double value, boolean callAction) Sets the value of the slider. Action is executed in the same thread.
Executes user action code (if there is any) if the callAction parameter is true.
If you call this function at runtime and set the new value out of the specified range, the slider's value will be corrected to the closest possible value inside the slider’s range and step grid and a warning will be displayed. If the user action code refers to the new value, the corrected value will be applied.
value — the new value.
callAction — if true, user action code will be executed (if there is any).void setValueToDefault() Sets the value of the slider to what was provided as the default one.
Doesn’t execute user action code. - Values range
-
Function Description double getMin() Returns the minimum value of the slider. double getMax() Returns the maximum value of the slider. void setRange(double min, double max) Sets the minimum and maximum values of the slider.
Does not execute user action code.
If the slider is configured to work with int values (linked to a variable or parameter of the int type), the given [min, max] range may be automatically corrected to have integer bounds (within the given double bounds). This function does nothing if the slider already has such range.
If the current value of the slider is out of the new specified range, it will be snapped to the closest possible value inside the slider's new range. The value of the linked parameter will remain unchanged.
min — the new minimum value.
max — the new maximum value.void setRange(double min, double max, boolean callAction) Sets the minimum and maximum values of the slider.
If the value of the current slider changes, the function will execute user action code (if there is any) in the same thread — if the callAction parameter is true.
If slider is configured to work with int values (linked to a variable or parameter of the int type), the given [min, max] range may be automatically corrected to have integer bounds (within the given double bounds). This function does nothing if the slider already has such range.
If the current value of the slider is out of the new specified range, it will be snapped to the closest possible value inside the slider's new range. The value of the linked parameter will remain unchanged.
min — the new minimum value.
max — the new maximum value.
callAction — if true, user action code will be executed if the current slider value changes (if there is any). - Step
-
Function Description double getStep() Returns the value of the slider’s step. void setStep(double step) Sets the value of the slider’s step.
If the current value of the slider is not on of the slider’s new step grid, it will be snapped to the closest possible value inside the slider’s new step grid. The value of the linked parameter will remain unchanged.
step — the new step value. - Enabling and disabling
-
Function Description boolean isEnabled() Checks whether the control is enabled or disabled. Returns true if enabled, otherwise false. void setEnabled(boolean yes) Sets the control to be 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 the slider. - 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 the X coordinate.void setY(double y) Sets the Y coordinate of the control.
y — the new value of the Y coordinate.void setPos(double x, double y) Sets new coordinates for the control.
x — the new value of the X coordinate.
y — the new value of the Y coordinate. - 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 visible, if false — not visible. - Group
-
Function Description ShapeGroup getGroup() Returns the group containing this control. - Level
-
Function Description Level getLevel() Returns the level on which this control is located.
-
How can we improve this article?
-