DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

J2ME - Alert Bloccante

02.24.2007
| 7332 views |
  • submit to reddit
        // description of your code here

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

public class MessageBox extends Alert implements CommandListener
{
	protected Chattando midlet;
	
	private boolean isReady = false;
	
	private Displayable dspBACK;
	
	public MessageBox(String title, String text, AlertType type, Chattando midlet)
	{
		super(title, text, null, type);
		
		this.midlet = midlet;
		
		this.setCommandListener(this);
		this.setTimeout(Alert.FOREVER);
		
		// Display Precedente
		dspBACK = midlet.getDisplay().getCurrent();
		
		// Mostra l'alert
		midlet.getDisplay().setCurrent(this);
		
		// Attendi la conferma di chiusura
		waitForDone();
		
		// Visualizza il precedente Display
		midlet.getDisplay().setCurrent(dspBACK);
	}
	
	private void waitForDone()
	{
		try
		{
			while(!isReady)
			{
				synchronized(this)
				{
					this.wait();
					
				}
			}
		}
		catch(Exception error)
		{
			
		}
	}

	public void commandAction(Command cmd, Displayable dsp)
	{
		if(cmd == Alert.DISMISS_COMMAND)
		{
			isReady = true;
			
			synchronized(this)
			{
				this.notify();
			}			
		}
	}
}