Home J2me J2ME HashTable Example

Related Tutorials


 
 

Share on Google+Share on Google+

J2ME HashTable Example

Advertisement
To use the HashTable, java.util.Hashtable package must be imported into the application.

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 != null) player.stop();
  } catch(Exception e){}
  }

  public void destroyApp(boolean unconditional){
  if(player != null) player.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 key) throws 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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 1, 2008

Related Tutorials

Discuss: J2ME HashTable Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com