Tuesday, December 4, 2012

Menggunakan objek List dengan tipe MULTIPLE pada JAVA (J2ME)

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






public class MultipleList extends MIDlet
  implements CommandListener {
   
  private Display display;
  private List list;
 
  private Command cmdPilih;
  private Command cmdKembali; 
 
  public ExclusiveList() {
      display = Display.getDisplay(this);
  }
 
  public void startApp() {
     
      // membuat daftar command yang akan digunakan
      cmdPilih = new Command("Pilih", Command.SCREEN, 2);
      cmdKembali = new Command("Kembali", Command.EXIT, 1);
           
      // membuat gambar icon untuk setiap item List
      Image img;
      try {
          img = Image.createImage("/app.png");
      } catch (Exception e) {
          img = null;
      }
     
      // membuat List dengan tipe IMPLICIT
      list = new List("Tipe MULTIPLE", Choice.MULTIPLE);
      list.append("Pilihan pertama", img);
      list.append("Pilihan kedua", img);
      list.append("Pilihan ketiga", img);
      list.append("Pilihan keempat", img);
      list.addCommand(cmdPilih);
      list.addCommand(cmdKembali);
      list.setCommandListener(this);
     
      display.setCurrent(list);
  }
 
  public void pauseApp() {
      //
  }
 
  public void destroyApp(boolean unconditional) {
      notifyDestroyed();
  }
 
  public void commandAction(Command c, Displayable s) {
      if (c == cmdKembali) {
          destroyApp(false);
      } else {
          boolean[] pilihan = new boolean[list.size()];
          list.getSelectedFlags(pilihan);
         
          String str = "";
          for (int i=0; i < pilihan.length; i++) {
              if (pilihan[i] == true) {             
                  str = str + list.getString(i) + '\n'; 
/* keterangan: '\n' = new line */   
              }
          }
         
          Alert info = new Alert("Informasi");
          info.setString(str);
          info.setTimeout(Alert.FOREVER);
          display.setCurrent(info, list);
      }
  }
}

Menggunakan objek List dengan tipe IMPLICIT pada JAVA (J2ME)

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

public class ImplicitList extends MIDlet
  implements CommandListener {
   
  private Display display;
  private List list;
 
  private Command cmdPilih;
  private Command cmdKembali; 
 
  public ExclusiveList() {
      display = Display.getDisplay(this);
  }
 
  public void startApp() {
     
      // membuat daftar command yang akan digunakan
      cmdPilih = new Command("Pilih", Command.SCREEN, 2);
      cmdKembali = new Command("Kembali", Command.EXIT, 1);
           
      // membuat gambar icon untuk setiap item List
      Image img;
      try {
          img = Image.createImage("/app.png");
      } catch (Exception e) {
          img = null;
      }
     
      // membuat List dengan tipe IMPLICIT
      list = new List("Tipe IMPLICIT", Choice.IMPLICIT);
      list.append("Pilihan pertama", img);
      list.append("Pilihan kedua", img);
      list.append("Pilihan ketiga", img);
      list.append("Pilihan keempat", img);
      list.addCommand(cmdPilih);
      list.addCommand(cmdKembali);
      list.setCommandListener(this);
     
      display.setCurrent(list);
  }
 
  public void pauseApp() {
      //
  }
 
  public void destroyApp(boolean unconditional) {
      notifyDestroyed();
  }
 
  public void commandAction(Command c, Displayable s) {
      if (c == cmdKembali) {
          destroyApp(false);
      } else {
          int indeks = list.getSelectedIndex();
          Alert info = new Alert("Informasi");
          info.setString("Anda memilih: " +
list.getString(indeks));
          info.setTimeout(Alert.FOREVER);
          display.setCurrent(info, list);
      }
  }
}

Menggunakan objek List dengan tipe EXCLUSIVE pada JAVA (J2ME)

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



public class ExclusiveList extends MIDlet
  implements CommandListener {
   
  private Display display;
  private List list;
 
  private Command cmdPilih;
  private Command cmdKembali; 
 
  public ExclusiveList() {
      display = Display.getDisplay(this);
  }
 
  public void startApp() {
     
      // membuat daftar command yang akan digunakan
      cmdPilih = new Command("Pilih", Command.SCREEN, 2);
      cmdKembali = new Command("Kembali", Command.EXIT, 1);
           
      // membuat gambar icon untuk setiap item List
      Image img;
      try {
          img = Image.createImage("/app.png");
      } catch (Exception e) {
          img = null;
      }
     
      // membuat List dengan tipe EXCLUSIVE
      list = new List("Tipe EXCLUSIVE", Choice.EXCLUSIVE);
      list.append("Pilihan pertama", img);
      list.append("Pilihan kedua", img);
      list.append("Pilihan ketiga", img);
      list.append("Pilihan keempat", img);
      list.addCommand(cmdPilih);
      list.addCommand(cmdKembali);
      list.setCommandListener(this);
     
      display.setCurrent(list);
  }
 
  public void pauseApp() {
      //
  }
 
  public void destroyApp(boolean unconditional) {
      notifyDestroyed();
  }
 
  public void commandAction(Command c, Displayable s) {
      if (c == cmdKembali) {
          destroyApp(false);
      } else {
          int indeks = list.getSelectedIndex();
          Alert info = new Alert("Informasi");
          info.setString("Anda memilih: " +
list.getString(indeks));
          info.setTimeout(Alert.FOREVER);
          display.setCurrent(info, list);
      }
  }

}

Monday, December 3, 2012

Menggunakan objek TextField pada JAVA (J2ME)

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

public class DemoTextField extends MIDlet
  implements CommandListener {
   
  private Display display;
  private Form form;
 
  private TextField nama, password, telp, email, URL;
 
  private final Command cmdKeluar =
    new Command("Keluar", Command.EXIT, 1);
   
  public DemoTextField() {
    display = Display.getDisplay(this);
  }
   
  public void startApp() {          
      
    form = new Form("Demo TextField");
       
    nama = new TextField("Nama:", null, 25, TextField.ANY);
    password = new TextField("Password:", null, 25,
                     TextField.PASSWORD);
    email = new TextField("Email:", null, 50,
                     TextField.EMAILADDR);
    URL = new TextField("URL:", null, 50, TextField.URL);
    telp = new TextField("Telepon:", null, 15,
                     TextField.PHONENUMBER);
   
    form.append(nama);
    form.append(password);
    form.append(email);
    form.append(URL);
    form.append(telp);
   
    form.addCommand(cmdKeluar);
    form.setCommandListener(this);
    
    display.setCurrent(form);
  }
   
  public void pauseApp() {
  }
   
  public void destroyApp(boolean unconditional) {
  }
   
  public void commandAction(Command c, Displayable s) {
    if (c == cmdKeluar) {
      destroyApp(false);
      notifyDestroyed();
    }
  } 
}

Menggunakan objek TextBox pada JAVA (J2ME)

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

public class DemoTextBox extends MIDlet
implements CommandListener {
   
  private Display display;
  private TextBox tb;
  private Form formInfo;
  private Alert alert;
  private Command cmdKeluar;
  private Command cmdSetTeks;
  private Command cmdSisipTeks;
  private Command cmdKosongkanTeks;
  private Command cmdInfoTeks;
  private Command cmdKembali;
   
  public DemoTextBox() {
    display = Display.getDisplay(this);
   
    tb = new TextBox("Contoh TextBox", null,
                     256, TextField.ANY);
               
    cmdKeluar  = new Command("Keluar", Command.EXIT, 1);
    cmdSetTeks = new Command("Set Teks", Command.SCREEN, 2);
    cmdSisipTeks = new Command("Sisipkan Teks",
                               Command.SCREEN, 2);
    cmdKosongkanTeks = new Command("Kosongkan Teks",
                                   Command.SCREEN, 2);
    cmdInfoTeks = new Command("Info Teks", Command.SCREEN, 2);

    cmdKembali = new Command("Kembali", Command.SCREEN, 2);

    tb.addCommand(cmdKeluar);
    tb.addCommand(cmdSetTeks);
    tb.addCommand(cmdSisipTeks);
    tb.addCommand(cmdKosongkanTeks);
    tb.addCommand(cmdInfoTeks);
    tb.setCommandListener(this);
  }
   
  public void startApp() {
    display.setCurrent(tb);
  }
   
  public void pauseApp() {
  }
   
  public void destroyApp(boolean unconditional) {
  }
   
  public void commandAction(Command c, Displayable s) {
    if (c == cmdKeluar) {
      destroyApp(true);
      notifyDestroyed();
    } else if (c == cmdSetTeks) {
      tb.setString("Contoh pengesetan teks di dalam TextBox");
    } else if (c == cmdSisipTeks) {
      tb.insert("TEKS SISIPAN", 0);
    } else if (c == cmdKosongkanTeks) {
      if (tb.size() > 0) {
        tb.delete(0, tb.size());
      }
    } else if (c == cmdInfoTeks) {
      formInfo = new Form("Informasi Teks");
      formInfo.append("Teks aktif: " + tb.getString() + "\n");
      formInfo.append("Jumlah karakter: " + tb.size() + "\n");
      formInfo.append("Posisi cursor: " + tb.getCaretPosition());
           
      formInfo.addCommand(cmdKembali);
      formInfo.setCommandListener(this);
      display.setCurrent(formInfo);
    } else if (c == cmdKembali) {
      display.setCurrent(tb);
    }
  }   
}

Membuat interface ItemStateListener pada JAVA (J2ME)

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Date;

public class DemoItemStateListener extends MIDlet
  implements CommandListener {
   
  private Display display;
  private Form form;

  private final Command cmdKeluar =
    new Command("Keluar", Command.EXIT, 1);
 
  public DemoItemStateListener() {
    display = Display.getDisplay(this);        
    form = new Form("Demo ItemStateListener");
    form.addCommand(cmdKeluar);
    form.setCommandListener(this);
  }
   
  public void startApp() {
    ItemStateListener listener = new ItemStateListener() {
      Calendar cal =
        Calendar.getInstance(TimeZone.getDefault()); 
       
      public void itemStateChanged(Item item) {
        cal.setTime(((DateField) item).getDate());
        Alert alert = new Alert("Informasi", null, null,
                        AlertType.INFO);
        alert.setTimeout(3000);
        alert.setString("Tanggal telah berhasil diubah.");
        display.setCurrent(alert, form);
      }     
    };
   
    form.setItemStateListener(listener);
    Date now = new Date();
    DateField dateItem = new DateField("Hari ini tanggal: ",
                           DateField.DATE);
    dateItem.setDate(now);
    form.append(dateItem);
    display.setCurrent(form);     
  }
   
  public void pauseApp() {
  }
   
  public void destroyApp(boolean unconditional) {
  }
   
  public void commandAction(Command c, Displayable s) {
    if (c == cmdKeluar) {
      destroyApp(false);
      notifyDestroyed();
    }
  }  
 
}

Membuat objek Gauge pada JAVA (J2ME)

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

public class DemoGauge extends MIDlet
  implements CommandListener {
   
  private Display display;
  private Form form;
  private Gauge gInteraktif, gNonInteraktif;
 
  private final Command cmdKeluar =
    new Command("Keluar", Command.EXIT, 1);
 
  public DemoGauge() {
    display = Display.getDisplay(this);
   
    form = new Form("Demo Gauge");
    form.addCommand(cmdKeluar);
    form.setCommandListener(this);
   
    gInteraktif = new Gauge("Interaktif", true, 10, 0);
    form.append(gInteraktif);
   
    gNonInteraktif = new Gauge("Non-interaktif", false, 10, 0);
    form.append(gNonInteraktif);
   
  }
   
  public void startApp() {                
    display.setCurrent(form);
  }
   
  public void pauseApp() {
  }
   
  public void destroyApp(boolean unconditional) {
  }
   
  public void commandAction(Command c, Displayable s) {
    if (c == cmdKeluar) {
      destroyApp(false);
      notifyDestroyed();
    }
  } 
}