Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

Hot Searches
struts-config.xml  web.xml  Java date format  read/write text file using javascript  ArrayList  Insert Data into Database Using Hibernat  create text file using java  insert data to text file using java  Visual Basic Date and Time Display the t  Create table and insert data by sql quer 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

J2ME HashTable Example

                         

To use the HashTable, java.util.Hashtable package must be imported into the application. Generally HashTable are used to map the keys to values and these keys or values could be any null object. 

Please go through the given code to see, how a HashTable can be used into the applications.

Output of the application will look like as follow..

 

 

 

 

Image 1

HashTable Example in J2ME

Image 2

 

Source Code of HashTableMIDlet.java

import java.util.*;
import java.util.Hashtable;
import javax.microedition.media.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class HashTableExample extends MIDlet implements CommandListener, PlayerListener{
  private Display display;
  private List itemList;
  private Form form;
  private Command stop, start, pause;
  private Hashtable items, itemsInfo;
  private Player player;

  public HashTableExample(){
    display = Display.getDisplay(this);
    itemList = new List("Select an item to play", List.IMPLICIT);
    stop = new Command("Stop", Command.STOP, 1);
    pause = new Command("Pause", Command.ITEM, 1);
    start = new Command("Start", Command.ITEM, 1);
    form = new Form("Playing media");
    form.addCommand(stop);
    form.addCommand(pause);
    form.setCommandListener(this);

    //** Implementing Hash Table **//

    items = new Hashtable();
    itemsInfo = new Hashtable();

    items.put("Police Siren""file://siren.wav");
    itemsInfo.put("Police Siren""audio/x-wav")

    items.put("Packman Sound""file://PacMan.wav");
    itemsInfo.put("Packman Sound""audio/x-wav")
  }

  public void startApp(){
    for(Enumeration en = items.keys(); en.hasMoreElements();){
      itemList.append((String)en.nextElement()null);
    }
    itemList.setCommandListener(this);
    display.setCurrent(itemList);
  }

  public void pauseApp(){
    try {
      if(player != nullplayer.stop();
    catch(Exception e){}
  }

  public void destroyApp(boolean unconditional){
    if(player != nullplayer.close();
  }

  public void commandAction(Command command, Displayable disp){
    if(disp instanceof List){
      List list = ((List)disp);
      String key = list.getString(list.getSelectedIndex());
      try{
        playMedia((String)items.get(key), key);
      catch (Exception e){
        System.err.println("Unable to play: " + e);
        e.printStackTrace();
      }
    else if(disp instanceof Form){
      try {
        if(command == stop){
          player.close();
          display.setCurrent(itemList);
          form.removeCommand(start);
          form.addCommand(pause);
        else if(command == pause){
          player.stop();
          form.removeCommand(pause)
          form.addCommand(start);
        else if(command == start){
          player.start();
          form.removeCommand(start);
          form.addCommand(pause);
        }
      catch(Exception e){
        System.err.println(e);
      }
    }
  }

  private void playMedia(String locator, String keythrows Exception{
    String file = locator.substring(locator.indexOf("file://"6
    locator.length
());
    player = Manager.createPlayer(getClass().getResourceAsStream(file)
(
String)itemsInfo.get(key));
    player.addPlayerListener(this);
    player.setLoopCount(-1);
    player.prefetch();
    player.realize();
    player.start();
  }

  public void playerUpdate(Player player, String event, Object eventData){
    if(event.equals(PlayerListener.STARTED&& new Long(0L).equals((Long)eventData)){
      display.setCurrent(form);
    else if(event.equals(PlayerListener.CLOSED)){
      form.deleteAll();
    }
  }
}

Download Source Code

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.