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:
