Follow Flex After Dark on Twitter

Alert Dialogs (mx.controls.Alert)

The Alert control is used to easily create a pop-up dialog. An Alert dialog box that can contain a message, a title, buttons (any combination of OK, Cancel, Yes, and No) and an icon. The ActionScript Alert class is in the mx.controls package.

The easiest way to show an Alert message is via the static show() function.

Display an Alert dialog via a static method call:

   // show an alert dialog
   Alert.show( "This is a message!", "Dialog Title" );

Alert Dialog

The Alert control is modal by default, which means it will retain focus until the user closes it. The Alert control closes when a button is selected or the Escape key is pressed.

Advanced Alerts

The Alert class can be used for more than displaying a simple mesage. Using the Alert class you can also ask

The static show() function takes in several arguments, many of them optional, detailed below.

Arguments to the show() function:

ArgumentTypeDefaultDescription
textString""The text to display in the dialog
titleString""The dialog title
flagsuintAlert.OKBit-flags that affect Alert options, such as which buttons are shown
parentSpritenullThe parent of the dialog (usually this will suffice)
closeHandlerFunctionnullThe function to be called when the Alert is closed
iconClassClassnullAn image or icon to show with the Alert
defaultButtonFlaguintAlert.OKA bit-flag to specify the default button

The flags argument to the show() function accepts bit-flags. The acceptable flags are constants of the Alert class:

  • OK - enables an OK button on the Alert control
  • YES - enables a Yes button on the Alert control
  • NO - enables a No button on the Alert control
  • CANCEL - enables a Cancel button on the Alert control
  • NONMODAL - makes the Alert non-modal

The flags argument is passed as "bit-or" seperated values. For example, passing Alert.YES | Alert.NO | Alert.CANCEL means show "Yes", "No", and "Cancel" buttons on the dialog.

Bit-flags allow you to pass several on/off options as a single argument to a function.

Examples using Alert

The Alert dialog can be used in a variety of situations.

Show a Yes or No Dialog

The Alert class can be used to easily prompt a user to answer a yes/no question. We'll use the Alert.YES and Alert.NO flags to show "Yes"/"No" buttons.

Show a Yes/No prompt with Alert:

   // show an alert with a question and yes and no choices
   Alert.show( "Would you like to choose yes or no?", "Question", 
      Alert.YES | Alert.NO, this, closeHandler, null, Alert.YES );

Alert Dialog

Note that the "Yes" button is the default button because we passed the Alert.YES value as the (last) defaultButtonFlag argument.

Show a Non-Modal Alert Dialog

By default an Alert dialog is modal, meaning it retains focus, blocking the background, until the user closes it. However, you can show a non-modal dialog by using the Alert.NONMODAL flag.

Show a non-modal Alert dialog:

   // show a non-modal alert
   Alert.show( "This is dialog is non-modal.", "Non-Modal", Alert.NONMODAL );

Advanced Dialogs beyond Alerts

Learn more about creating advanced dialogs in Flex...

Related Links:

For more information on the Alert API, checkout the Flex reference docs for the Alert class.

Related Docs
  • Flex Dialogs Overview of Dialogs in Flex and usage examples.
Recent Docs
  • ActionScript Alerts The ActionScript Alert control can be used to create interactive dialogs easily.
  • Flex StyleSheets StyleSheets define styles for Flex components and can be defined inline or loaded at run-time.
  • Flex Containers Overview of Flex Containers and Layouts.
  • Flex Binding Flex Data Binding enables objects and their values to be bound together so that when a source changes a target automatically gets updated.
  • Object-Oriented ActionScript ActionScript is a full-featured Object-Oriented language with support for classes, interfaces, encapsulation, polymorphism, and inheritance.
Random Docs
  • Flex Full Screen Using Full Screen mode for Flex applications.
  • ActionScript Alerts The ActionScript Alert control can be used to create interactive dialogs easily.
  • Flex Libraries A Library is collection of Flex/ActionScript classes and resources compiled to a .swc file.
  • Flex Containers Overview of Flex Containers and Layouts.
  • ActionScript Functions ActionScript Functions are first-class objects and can be registered as event listeners.