J2ME Icon MIDlet Example

In this example we are going to create icon list of different sizes. We are
creating here a IconList class, in this class we inherited the property of List
class. Then we are creating the constructor of IconList class on the basis of
this predefined constructure.
List(String title, int listType, String[] stringElements, Image[]
imageElements)
In this constructor we Creates a new List, specifying with its title, the
type of the List, an array of string element and an array of image element.
In the Icon MIDlet class we are creating five images with its label. The
Application is as follows:

IconMIDlet.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.IOException;
public class IconMIDlet extends MIDlet{
private Display display;
private IconList iconList;
private Image icon16x16, icon18x18, icon20x20, icon22x22, icon32x32;
private String[] label = {"16x16 pixels", "18x18 pixels", "20x20 pixels",
"22x22 pixels", "32x32 pixels"};
public IconMIDlet(){
try{
icon16x16 = Image.createImage("/16.png");
icon18x18 = Image.createImage("/18.png");
icon20x20 = Image.createImage("/20.png");
icon22x22 = Image.createImage("/22.png");
icon32x32 = Image.createImage("/32.png");
}catch (IOException ioe){
ioe.printStackTrace();
}
}
protected void startApp(){
Image[] listIcon = {icon16x16, icon18x18, icon20x20, icon22x22, icon32x32};
iconList = new IconList("IconList", List.IMPLICIT, label, listIcon, this);
iconList.setFitPolicy(List.TEXT_WRAP_OFF);
display = Display.getDisplay(this);
display.setCurrent(iconList);
}
protected void pauseApp(){}
protected void destroyApp(boolean unconditional){
notifyDestroyed();
}
}
class IconList extends List implements CommandListener{
private IconMIDlet midlet;
private Command exit;
public IconList(String title, int type, String[] elements, Image[]
images, IconMIDlet midlet){
super(title, type);
this.midlet = midlet;
for (int i = 0; i < elements.length; i++) {
this.append(elements[i], images[i]);
}
exit = new Command("Exit", Command.EXIT, 1);
this.addCommand(exit);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable d){
if (c == exit) {
midlet.destroyApp(true);
}
}
}
|
Download Source Code

|