Tuesday, June 30, 2009

J2ME programming : The GUI Layout Program

Dear folks here i am presenting a source code which is the entry point of many gui based application , this is made in netbeans

hear goes the source code

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package loginscreenexample;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.LoginScreen;
import org.netbeans.microedition.lcdui.SplashScreen;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
* @author rohit
*/
public class loginscreenexample1 extends MIDlet implements CommandListener {

private boolean midletPaused = false;

//
private SplashScreen splashScreen;
private WaitScreen waitScreen;
private LoginScreen loginScreen;
private Alert alertFailure;
private Alert alertSuccess;
private Command exitCommand;
private SimpleCancellableTask task;
//


/**
* The loginscreenexample1 constructor.
*/
public loginscreenexample1() {
}

//
//


//
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the startMIDlet method.
*/
private void initialize() {
// write pre-initialize user code here

// write post-initialize user code here
}
//


//
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
// write pre-action user code here
switchDisplayable(null, getSplashScreen());
// write post-action user code here
}
//


//
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {
// write pre-action user code here
switchDisplayable(null, getLoginScreen());
// write post-action user code here
}
//


//
/**
* Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//


//
/**
* Called by a system to indicated that a command has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// write pre-action user code here
if (displayable == loginScreen) {
if (command == LoginScreen.LOGIN_COMMAND) {
// write pre-action user code here
switchDisplayable(null, getWaitScreen());
// write post-action user code here
} else if (command == exitCommand) {
// write pre-action user code here

// write post-action user code here
}
} else if (displayable == splashScreen) {
if (command == SplashScreen.DISMISS_COMMAND) {
// write pre-action user code here
switchDisplayable(null, getLoginScreen());
// write post-action user code here
}
} else if (displayable == waitScreen) {
if (command == WaitScreen.FAILURE_COMMAND) {
// write pre-action user code here
switchDisplayable(getAlertFailure(), getWaitScreen());
// write post-action user code here
} else if (command == WaitScreen.SUCCESS_COMMAND) {
// write pre-action user code here
switchDisplayable(getAlertSuccess(), getWaitScreen());
// write post-action user code here
}
}
// write post-action user code here
}
//


//
/**
* Returns an initiliazed instance of splashScreen component.
* @return the initialized component instance
*/
public SplashScreen getSplashScreen() {
if (splashScreen == null) {
// write pre-init user code here
splashScreen = new SplashScreen(getDisplay());
splashScreen.setTitle("splashScreen");
splashScreen.setCommandListener(this);
splashScreen.setText("Login Screen\n");
// write post-init user code here
}
return splashScreen;
}
//


//
/**
* Returns an initiliazed instance of waitScreen component.
* @return the initialized component instance
*/
public WaitScreen getWaitScreen() {
if (waitScreen == null) {
// write pre-init user code here
waitScreen = new WaitScreen(getDisplay());
waitScreen.setTitle("waitScreen");
waitScreen.setCommandListener(this);
waitScreen.setText("\"Please Wait...\"");
waitScreen.setTask(getTask());
// write post-init user code here
}
return waitScreen;
}
//


//
/**
* Returns an initiliazed instance of loginScreen component.
* @return the initialized component instance
*/
public LoginScreen getLoginScreen() {
if (loginScreen == null) {
// write pre-init user code here
loginScreen = new LoginScreen(getDisplay());
loginScreen.setLabelTexts("Username", "Password");
loginScreen.setTitle("loginScreen");
loginScreen.addCommand(LoginScreen.LOGIN_COMMAND);
loginScreen.addCommand(getExitCommand());
loginScreen.setCommandListener(this);
loginScreen.setBGColor(-3355444);
loginScreen.setFGColor(0);
loginScreen.setUseLoginButton(false);
loginScreen.setLoginButtonText("Login");
// write post-init user code here
}
return loginScreen;
}
//


//
/**
* Returns an initiliazed instance of alertFailure component.
* @return the initialized component instance
*/
public Alert getAlertFailure() {
if (alertFailure == null) {
// write pre-init user code here
alertFailure = new Alert("alert", "Error", null, null);
alertFailure.setTimeout(Alert.FOREVER);
// write post-init user code here
}
return alertFailure;
}
//


//
/**
* Returns an initiliazed instance of alertSuccess component.
* @return the initialized component instance
*/
public Alert getAlertSuccess() {
if (alertSuccess == null) {
// write pre-init user code here
alertSuccess = new Alert("alert1", "", null, null);
alertSuccess.setTimeout(Alert.FOREVER);
// write post-init user code here
}
return alertSuccess;
}
//


//
/**
* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {
// write pre-init user code here
exitCommand = new Command("Exit", Command.EXIT, 0);
// write post-init user code here
}
return exitCommand;
}
//


//
/**
* Returns an initiliazed instance of task component.
* @return the initialized component instance
*/
public SimpleCancellableTask getTask() {
if (task == null) {
// write pre-init user code here
task = new SimpleCancellableTask();
task.setExecutable(new org.netbeans.microedition.util.Executable() {
public void execute() throws Exception {
// write task-execution user code here
}
});
// write post-init user code here
}
return task;
}
//


/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}

/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}

/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}

/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}

/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}

}

No comments: