It is a screen mostly like message box appear public Alert ... · It is a screen mostly like...

Post on 04-Aug-2020

2 views 0 download

Transcript of It is a screen mostly like message box appear public Alert ... · It is a screen mostly like...

It is a screen mostly like message box appear to user as monition about specific procedure

This object have two constructors :

public Alert(String title)

Alert(String title, String alertText, Image alertImage, AlertType alertType)

Title : appear above the box alertText : the text appear inside alert box and

the user read it alertImage : icon or image appear above the

alert box, write null if you don’t want image alertType : type of alert is one of these

WARNING , INFO , ERROR , CONFIRMATION , ALARM And the type written as follow AlertType.TYPE , replacing TYPE by one of

alert types

public void addCommand(Command cmd)

public void setString(String str)

public void setTimeout(int time)

Set the time for which the Alert is to be shown. This must either be a positive time value in milliseconds, or the special value FOREVER.

Parameters : time - timeout in milliseconds, or FOREVER

Example : alert.setTimeout(5000);

If we want to stay the show forever it done by written FOREVER in the Alert class

alert.setTimeout(Alert.FOREVER)

Example explain alert types and how to create it :

import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class MyAlert extends MIDlet implements CommandListener { private Display display; Command cmdExit = new Command( "Exit“ , Command.EXIT, 0); Command cmdAlert = new Command("Alert", Command.SCREEN, 0); Command cmdAlertError = new Command("Error", Command.SCREEN, 0);

Command cmdAlertSave = new Command("Save", Command.SCREEN, 0); Form form; Alert alert; public MyAlert () { display =Display.getDisplay(this); form = new Form("Form Title"); alert = new Alert ( " Alert Title “ ); }

public void startApp() { form.append("This is My Form"); form.addCommand(cmdExit); form.addCommand(cmdAlert); form.addCommand(cmdAlertError); form.addCommand(cmdAlertSave); form.setCommandListener(this); display.setCurrent(form);

}

public void pauseApp(){ } public void destroyApp(boolean unconditional){} /************* implements CommandListener ****************/ public void commandAction(Command c, Displayable s) { if ( s == form ) { if (c == cmdExit) { destroyApp(false); notifyDestroyed(); }

else if (c == cmdAlert) { alert.setString("Alert with command Ignore\n _____\n Alarm Type "); alert.setType(AlertType.ALARM); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert);} else if (c == cmdAlertError) { alert.setString("Alert with 5 second waitting \n _____\n Error Type"); alert.setType(AlertType.ERROR); alert.setTimeout(5000); display.setCurrent(alert); }

else if (c == cmdAlertSave) { alert.setString("Alert with 3 second waitting

\n _____\n Confirmation Type"); alert.setType(AlertType.CONFIRMATION); alert.setTimeout(3000); display.setCurrent(alert); }} } // end commandAction

} // end Class

Example explain : (Command cmdAlert = new Command("Alert",

Command.SCREEN, 0);

Command cmdAlertSave = new Command ("Save", Command.SCREEN,0);

Command cmdAlertError = new Command("Error", Command.SCREEN,0);

Define three commands and will used to show Alert types

Form form;

used to show commands on it

Alert alert;

Alert appear on the screen and will change the type

public MyAlert () {

put the initial statement in the constructor

alert = new Alert (" Alert Title ");

Alert Title

Create object from Alert type and give it a title

{

form.append("This is My Form");

Add text to the form

form.addCommand(cmdExit);

form.addCommand(cmdAlert);

form.addCommand(cmdAlertError);

form.addCommand(cmdAlertSave);

Add commands to form

else if (c == cmdAlert)

choose this commands

alert.setString("Alert with command Ignore\n _____\n Alarm Type ");

Text appear in the alert object

alert.setType(AlertType.ALARM);

Set the type of alert ALARM

alert.setTimeout(Alert.FOREVER); this alert will stay until user press done or ignore display.setCurrent(alert); show alert on screen after gave it’s properties else if (c == cmdAlertError) If we choose this command alert.setString("Alert with 5 second waitting

\n _____\n Error Type");

Show this text in alert object

alert.setType(AlertType.ERROR);

Set alert type ERROR and it like to alert “saved”

alert.setTimeout(3000);

Alert will appear just three seconds

display.setCurrent(alert);

Show this alert on screen