Alert

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.

Figure 2.8. The Alert class hierarchy


Table 2.7 shows the attributes of the ModalDialog class and Table 2.8 presents those of the Alert class.

Table 2.7. The attributes defined in the ModalDialog class
NameUsageTypeDefaultAccessibility
content_inset_bottomTag and JSnumber10read-write
 Description. The bottom inset for the content in pixels.
content_inset_leftTag and JSnumber14read-write
 Description. The left inset for the content in pixels.
content_inset_rightTag and JSnumber14read-write
 Description. The right inset for the content in pixels.
content_inset_topTag and JSnumber10read-write
 Description. The top inset for the content in pixels.

Table 2.8. The attributes defined in the Alert class
NameUsageTypeDefaultAccessibility
button1Tag and JSstringOKread-write
 Description. The text to be displayed on the first button on the Alert box.
button2Tag and JSstring read-write
 Description. The text for the second button on the Alert box.
maxtextwidthTag and JSnumber 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.
onresultTag and JSexpressionnullread-write
 Description. Invoked when a button is pressed.
resultTag and JSboolean 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_xTag and JSnumber0read-write
 Description. The x position of the button text.
text_yTag and JSnumber0read-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.

Listing 2.7. Using 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.

Figure 2.9. The Alert box


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”

Listing 2.8. An Alert box with two buttons
<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:

Figure 2.10. An Alert box with two buttons


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset