Home > J2ME-Mobile Programming > J2ME: Memanfaatkan Class TextBox

J2ME: Memanfaatkan Class TextBox

Class TextBox merupakan turunan dari class Screen yang merepresentasikan sebuah kotak untuk mengisi teks.

Konstruktor class TextBox adalah sebagai berikut:

TextBox(String title, String text, int maxSize, int constraints);

Agar lebih jelas untuk memahami tentang class TextBox berikut contoh programnya:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
public class MIDTextBox extends MIDlet implements CommandListener {
 
    private Display display;
    private TextBox txt;
    private Form form;
    private Alert alert;
    private Command cmdExit;
    private Command cmdSetText;
    private Command cmdInsertText;
    private Command cmdClearText;
    private Command cmdInfoText;
    private Command cmdBack;
 
    public MIDTextBox() {
        display = Display.getDisplay(this);
        txt = new TextBox("Contoh Midlet TextBox", null, 256, TextField.ANY);
        cmdExit = new Command("Exit", Command.EXIT, 1);
        cmdSetText = new Command("Set Text", Command.SCREEN, 2);
        cmdInsertText = new Command("Insert", Command.SCREEN, 2);
        cmdInfoText = new Command("Info Text", Command.SCREEN, 2);
        cmdBack = new Command("Back", Command.BACK, 2);
 
        txt.addCommand(cmdExit);
        txt.addCommand(cmdSetText);
        txt.addCommand(cmdInsertText);
        txt.addCommand(cmdInfoText);
        txt.setCommandListener(this);
    }
 
    public void startApp() {
        display.setCurrent(txt);
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
 
    public void commandAction(Command c, Displayable d) {
        if (c == cmdExit) {
            destroyApp(true);
            notifyDestroyed();
        } else if (c == cmdSetText) {
            txt.setString("Contoh Mengeset Text dalam TextBox");
        } else if (c == cmdInsertText) {
            txt.insert("Text Sisipan", 0);
        } else if (c == cmdClearText) {
            if (txt.size() > 0) {
                txt.delete(0, txt.size());
            }
        } else if (c == cmdInfoText) {
            form = new Form("Informasi Text");
            form.append("Teks Aktif: " + txt.getString() + "\n");
            form.append("Jumlah Karakter: " + txt.size() + "\n");
            form.append("Posisi cursor: " + txt.getCaretPosition());
 
            form.addCommand(cmdBack);
            form.setCommandListener(this);
            display.setCurrent(form);
        } else if (c == cmdBack) {
            display.setCurrent(txt);
        }
    }
}

Hasil program di atas adalah sebagai berikut:

Tampilan Awal

Tampilan Awal

Setelah muncul gambar di atas, dapat dicoba diklik “Menu”, hasilnya adalah:

Pilihan Menu Aplikasi

Pilihan Menu Aplikasi

Coba pilih menu Info Text, hasilnya adalah:

Tampilan menu "Info Text"

Tampilan menu "Info Text"

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  1. pung
    August 3rd, 2009 at 02:05 | #1

    apa to iki pak dosen?

  2. myandisun
    August 3rd, 2009 at 10:35 | #2

    Itu code program buat J2ME untuk program aplikasi Mobile.

  3. August 31st, 2009 at 10:33 | #3

    Nice mas. Bagus buat pemula. Kayanya mahasiswa saya harus baca blog ini.

  4. myandisun
    September 3rd, 2009 at 02:13 | #4

    Thnx’s semoga bermanfaat.

  1. No trackbacks yet.