An Alert object represents a modal dialog box to display a message. An Alert box comes equipped with an OK button and by default is not displayed. You must call its open method to make it visible.
The Alert class is a subclass of the ModalDialog class, which is derived from WindowPanel. A ModalDialog object represents a moveable floating view. When a modal dialog is open, no other part of the application is accessible, until the user closes the dialog.
Figure 2.8 shows the Alert class hierarchy.
Table 2.7 shows the attributes of the ModalDialog class and Table 2.8 presents those of the Alert class.
Name | Usage | Type | Default | Accessibility |
---|---|---|---|---|
content_inset_bottom | Tag and JS | number | 10 | read-write |
Description. The bottom inset for the content in pixels. | ||||
content_inset_left | Tag and JS | number | 14 | read-write |
Description. The left inset for the content in pixels. | ||||
content_inset_right | Tag and JS | number | 14 | read-write |
Description. The right inset for the content in pixels. | ||||
content_inset_top | Tag and JS | number | 10 | read-write |
Description. The top inset for the content in pixels. |
Name | Usage | Type | Default | Accessibility |
---|---|---|---|---|
button1 | Tag and JS | string | OK | read-write |
Description. The text to be displayed on the first button on the Alert box. | ||||
button2 | Tag and JS | string | read-write | |
Description. The text for the second button on the Alert box. | ||||
maxtextwidth | Tag and JS | number | read-write | |
Description. The maximum length of a line of text before it wraps. The default is one third of the parent’s width–the width of the window dressing and margin. | ||||
onresult | Tag and JS | expression | null | read-write |
Description. Invoked when a button is pressed. | ||||
result | Tag and JS | boolean | read-write | |
Description. The value of this attribute is set after the user closes this Alert box. The value is true is the user pressed the first button, and false if the user pressed the second button. | ||||
text_x | Tag and JS | number | 0 | read-write |
Description. The x position of the button text. | ||||
text_y | Tag and JS | number | 0 | read-write |
Description. The y position of the button text. |
There are two methods defined in the ModalDialog class:
close()
Closes this dialog.
open()
Opens the dialog and makes it visible.
For example, the code in Listing 2.7 shows how to use the Alert box.
<canvas> <alert name="warning">Press OK to continue.</alert> <script> canvas.warning.open(); </script> </canvas> |
To test this application, invoke the following URL:
http://localhost:8080/lps-4.0.x/app02/alertTest1.lzx
The generated output is shown in Figure 2.9.
As another example, the code in Listing 2.8 shows an Alert box with two buttons, the Yes button and the No button. If the OK button is pressed, “You said ‘Yes’” is displayed. Otherwise, “You said ‘No’” is shown. Note that the code in Listing 2.3 uses an event handler, which we will discuss in Chapter 5, “Event Handling”
<canvas> <alert name="warning" button1="Yes" button2="No"> Do you want to continue? <handler name="onresult"> if (this.result) { parent.message.setText("You said 'Yes'"); } else { parent.message.setText("You said 'No'"); } </handler> </alert> <script> canvas.warning.open(); </script> <text name="message">What do you think?</text> </canvas> |
You can run the application by directing your browser to this URL:
http://localhost:8080/lps-4.0.x/app02/alertTest2.lzx
The result is shown in Figure 2.10: