Search This Blog

Friday, 18 August 2023

Experiment 4 - Developing a MIDlet for Selecting or Deselection of Languages

0 comments

 Creating a MIDlet for Displaying a List of Languages Known

(Experiment 4)



Program:

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*;
public class MenuMidlet extends MIDlet implements CommandListener 

public ChoiceGroup ch;
public Form form; 
public  Display display; 
public  Command cmdView, cmdSelAll, cmdDesAll, cmdExit;
public StringItem options; 
public Item item;
public MenuMidlet()
{
display=Display.getDisplay(this); 
form = new Form("Languages Known");
ch = new ChoiceGroup("Prog. Languages",Choice.MULTIPLE); 
ch.append("C",null);
ch.append("C++",null); 
ch.append("Java",null); 
ch.append("Python",null); 
ch.setSelectedIndex(0, true); 
form.append(ch);
options=new StringItem("The Selected Languages are:","C");
cmdView = new Command("View",Command.OK,1); 
cmdSelAll = new Command("Select All",Command.OK,2);
cmdDesAll = new Command("Unselect All",Command.OK,3);
cmdExit = new Command("Exit",Command.EXIT,4); 
form.addCommand(cmdView); 
form.addCommand(cmdSelAll);
form.addCommand(cmdDesAll);
form.addCommand(cmdExit); 
form.append(options);
form.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 == cmdView)
{
boolean opt[]=new boolean[ch.size()]; 
String values=""; 
ch.getSelectedFlags(opt); 
options.setText("");
for(int i=0;i<opt.length;i++)
{
if(opt[i])
{
values+=ch.getString(i)+"\n";
}
}
options.setText(values); 
form.append(options); 
display.setCurrent(form);
}
else if(command == cmdSelAll)
{
int i=0;
int size=ch.size(); 
while(i<size)
{
ch.setSelectedIndex(i, true);
i++;
}
}
else if(command == cmdDesAll)
{
int i=0;
int size=ch.size(); 
while(i<size)
{
ch.setSelectedIndex(i, false);
i++;
}
}
else if(command==cmdExit)
{
destroyApp(true); 
notifyDestroyed();
}
}
}


Output:















Leave a Reply