Wednesday, July 22, 2009

Creating Buttons on J2ME Forms

StringItem can be used in order to create buttons on J2ME forms. Define a new Command variable in your Form and set it as defaultCommand for StringItem variable.

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;

public class FormWithButton extends Form implements ItemCommandListener{

private StringItem btnLogin;
private Command cmdLogin;
public FormWithButton(String str) {
super(str);
btnLogin = new StringItem(null, "Login");
cmdLogin = new Command("", Command.OK, 1);
btnLogin.setDefaultCommand(cmdLogin);
btnLogin.setItemCommandListener(this);
append(btnLogin);
}
public void commandAction(Command cmd, Item item) {
LoginForm.show();
}
}

1 comment:

  1. import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import java.io.*;

    class Krs extends Form implements CommandListener {

    Displayable krs;
    private final Command exitCommand = new Command ("KEMBALI", Command.EXIT, 1);
    private final Command connectCommand = new Command ("SIMPAN", Command.SCREEN, 2);
    private final Command finishCommand = new Command ("FINISH", Command.SCREEN, 3);
    private final Command tambahCommand = new Command ("TAMBAH", Command.BACK, 4);
    private Display display;
    private String pesan;
    private TextField t1,t2,t3,t4,t5;
    private Ticker ticker;
    private String teksTicker = "! APABILA TERJADI PERUBAHAN JADWAL, PIHAK KAMPUS AKAN MENGINFORMASIKAN MELALUI WEB) !";
    Form f2;
    Reg reg;
    private Finish finish;

    public Krs (Reg reg, Display display)
    {
    super ("PENGISIAN KRS");
    this.reg = reg;
    this.display = display;

    ticker = new Ticker(teksTicker);
    this.setTicker(ticker);

    t1 = new TextField ("Id Mahasiswa : ","",4,TextField.NUMERIC);
    t2 = new TextField ("Kode Mata Kuliah : ","",7,TextField.ANY);
    t3 = new TextField ("Kelas : ","",2,TextField.ANY);
    t4 = new TextField ("Ketua Jurusan : ","",11,TextField.NUMERIC);
    t5 = new TextField ("Dosen PA : ","",11,TextField.NUMERIC);
    this.append (t1);
    this.append (t2);
    this.append (t3);
    this.append (t4);
    this.append (t5);
    this.addCommand (exitCommand);
    this.addCommand (connectCommand);
    this.addCommand (finishCommand);
    this.setCommandListener(this);
    display.setCurrent(this);

    }

    public void commandAction(Command c, Displayable d)
    {
    String data = c.getLabel();
    if(c==exitCommand)
    {
    display.setCurrent(reg);
    }
    else if(c==connectCommand)
    {
    Thread t = new Thread()
    {
    public void run()
    {
    doDownload();
    }
    };
    t.start();
    }
    else if(c==finishCommand)
    {
    finish = new Finish (this, display);
    display.setCurrent(finish);
    }
    else if(c==tambahCommand)
    {
    awal();
    }

    }

    public void doDownload(){
    f2 = new Form ("INFO SERVER");
    pesan="";
    String URLsite = "http://127.0.0.1/krs/log3.php?";
    String p1,p2;
    p1 = "idmhs="+t1.getString()+"&kodemk="+t2.getString();
    p2 = "&kelas="+t3.getString()+"&iddosenkajur="+t4.getString()+"&iddosenpa="+t5.getString();
    HttpConnection con = null;
    InputStream in = null;
    StringBuffer data = new StringBuffer();

    try{
    con = (HttpConnection)Connector.open(URLsite+p1+p2);
    in = con.openInputStream();
    int ch;
    while((ch = in.read()) != -1){
    data.append((char)ch);
    }
    pesan = data.toString();
    f2.append(pesan);
    f2.addCommand(tambahCommand);
    f2.setCommandListener(this);
    display.setCurrent(f2);
    } catch (IOException e)
    {
    }
    }

    }






    script above command does not return its current path into the form f2. .

    how to get back to class krs, when it is entered in the form f2? help me

    ReplyDelete