Search This Blog

Monday, 28 August 2023

Experiment 5 - Displaying Multiple Screens with High-level UI

0 comments

Navigating through Multiple Screens in a MIDlet

(Experiment 5)

Version 1:

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*;

public class SlideShowText extends MIDlet implements CommandListener 
private Display display; 
private Command back; 
private Command exit; 
private Command nxtBtn;
private Command cmdDisp;

private Form form;
private TextBox txtSlide; 
private Form frm;
private TextField txtHello;
private StringItem st = new StringItem("","");

public SlideShowText() 
display = Display.getDisplay(this); 
exit = new Command("Exit", Command.EXIT, 1); 
nxtBtn = new Command(">>", Command.OK, 2);
back = new Command("<<", Command.BACK, 3); 

cmdDisp = new Command("Display",Command.OK,4);

/*  Code for Creating Screen1   */
form = new Form("Slide Show MIDlet - Home");
String msg = "Hello World!!!!!!!";
form.append(msg);
form.addCommand(exit); 
form.addCommand(nxtBtn); 
form.setCommandListener(this); 

/*  Code for Creating Screen2   */
txtSlide = new TextBox("Second Screen of Slide Show", "Press Prev Button to go back to Home screen & Next Button to go to Third Screen.", 81, 0); 
txtSlide.addCommand(back);
txtSlide.addCommand(nxtBtn); 
txtSlide.setCommandListener(this); 

/*  Code for Creating Screen3   */
frm = new Form("Third Screen of Slide Show");
txtHello = new TextField("Welcome Text", msg, 50, 0);
frm.append(txtHello);
frm.addCommand(back);
frm.addCommand(cmdDisp);
frm.addCommand(exit);
frm.setCommandListener(this);

public void startApp() 
display.setCurrent(form); 

public void pauseApp() 
{ }

public void destroyApp(boolean unconditional) 
{ } 

public void commandAction(Command command, Displayable displayable) 
if (command == back) 
   if (displayable == frm)
   { 
       display.setCurrent(txtSlide);
   }
   else
   {
       display.setCurrent(form);;
   }
   
else if (command == exit) 
destroyApp(false); 
notifyDestroyed(); 
else if (command == nxtBtn) 
    if (displayable == form)
    {
        display.setCurrent(txtSlide);
    }
    else if (displayable == txtSlide) 
    {
        display.setCurrent(frm); 
    }
}
else if (command == cmdDisp)
{
    st.setText(""); 
    st.setText(txtHello.getString()); 
    frm.append(st);
}
}
}

Output:








Version 2 (Displaying One after the Other like Slideshow):

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*;
public class SlideShow extends MIDlet implements CommandListener { 
public Form slide1; 
public Form slide2; 
public Form slide3; 
public Command Exit; 
public Display display;
public SlideShow()
{
display=Display.getDisplay(this);
Exit=new Command("Exit",Command.EXIT,1); 
slide1=new Form("Slide1"); 
slide1.append("This is Slide number 1"); 
slide1.addCommand(Exit);
slide2=new Form("Slide2"); 
slide2.append("This is Slide number 2"); 
slide2.addCommand(Exit);
slide3=new Form("Slide3"); 
slide3.append("This is Slide number 3"); 
slide3.addCommand(Exit); 
slide1.setCommandListener(this); 
slide2.setCommandListener(this); 
slide3.setCommandListener(this);
}
public void startApp() {
Thread runner = new Thread(new ThreadRunner(display,slide1,slide2,slide3)); 
runner.start();
}
public void pauseApp() 
{
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command,Displayable displayable)
{
if(displayable==slide1)
{
if(command==Exit) notifyDestroyed();
}
else if(displayable==slide2)
{
if(command==Exit) notifyDestroyed();
}
else if(displayable==slide3)
{
if(command==Exit) notifyDestroyed();
}
}
}
class ThreadRunner implements Runnable 
{
Display display; 
public int c=0; 
public Form slide1; 
public Form slide2; 
public Form slide3;
public ThreadRunner(Display display,Form slide1,Form slide2,Form slide3) 

this.display = display; 
this.slide1=slide1;
this.slide2=slide2; 
this.slide3=slide3;
}
public void run() 

while(true)

c++;
if(c==1)
display.setCurrent(slide1); 
else if(c==2)
display.setCurrent(slide2); 
else if(c==3)
display.setCurrent(slide3); 
else if(c==4)
c=0;
try
{
Thread.sleep(1500);
}
catch(Exception ex)
{ }
}

}

Output Screens:











































Leave a Reply