Emerging Technology
Fall Semester, 2004 MIDlet UI |
||
---|---|---|
© 2004, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 14-Oct-04 |
CS 683 Emerging Technologies Fall Semester, 2004 Doc 19 MIDlet UI
Copyright ©, All rights reserved. 2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 2 |
J2ME in a Nutshell, Kim Topley, O’Reilly, 2002, Chapter 3 & 4
Examples in this lecture are based on examples in the above reference
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 3 |
Embedding UI text strings in code is a poor idea
Put them in files
Add the files to the application jar file
Files
Hello World from a file
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 4 |
import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.*; public class HelloMIDlet extends MIDlet { public HelloMIDlet() { Form form = new Form( "Hi Mom" ); form.append( readFile() ); Display.getDisplay(this).setCurrent( form ); } private String readFile() { try { InputStream byteInput = getClass().getResourceAsStream("/Text.txt"); InputStreamReader characterInput = new InputStreamReader(byteInput); char[] buffer = new char[32]; StringBuffer stringBuffer = new StringBuffer(); int count; while ((count = characterInput.read(buffer, 0, buffer.length)) > -1) { stringBuffer.append(buffer, 0, count); } return stringBuffer.toString(); } catch (Exception ioProblem) { return "Could not read file"; } }
CS 683 Fall 04 Doc 19, MIDlet UI Slide # 5
public void startApp() {} public void pauseApp() { } public void destroyApp( boolean unconditional ) { } }
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 6 |
getClass().getResourceAsStream("/foo/bar/Text.txt");
Path is from the top level of the jar file
getClass().getResourceAsStream("/foo/bar/Text.txt");
Path is from the location of the location of the class in the jar file
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 7 |
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 8 |
Textbox Documemtation
Input Constraints
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 9 |
Based on example, pages 89-90, J2ME in a Nutshell, Kim Topley, O’Reilly
import java.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; public class TextBoxMIDlet extends MIDlet { private static final int MAX_TEXT_SIZE = 80; protected TextBox textBox; protected Display display; public TextBoxMIDlet() { textBox = new TextBox("TextBox Example", readFile("/Text.txt"), MAX_TEXT_SIZE, TextField.ANY); Ticker ticker = new Ticker("This is a ticker..."); textBox.setTicker(ticker); display = Display.getDisplay(this); display.setCurrent(textBox); } public void startApp() { } public void pauseApp() { } public void destroyApp( boolean unconditional ) { }
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 10 |
private String readFile(String filename) { try { InputStream byteInput = getClass().getResourceAsStream(filename); InputStreamReader characterInput = new InputStreamReader(byteInput); char[] buffer = new char[32]; StringBuffer stringBuffer = new StringBuffer(); int count; while ((count = characterInput.read(buffer, 0, buffer.length)) > -1) { stringBuffer.append(buffer, 0, count); } return stringBuffer.toString(); } catch (Exception ioProblem) { return "Could not read file"; } } }
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 11 |
Based on examples, pages 73-74, 89-90, J2ME in a Nutshell
import java.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; import java.util.*; public class ThreadTextBoxMIDlet extends MIDlet { private static final int MAX_TEXT_SIZE = 80; protected TextBox textBox; private Timer timer; private int count = 0; public ThreadTextBoxMIDlet() { textBox = new TextBox("Timer Example", "Hello for now", MAX_TEXT_SIZE, TextField.ANY); Display.getDisplay(this).setCurrent(textBox); } public void startApp() { if (timer == null) startTimer(); else count = 0; }
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 12 |
public void pauseApp() { textBox.setString("Here we go again"); } public void destroyApp( boolean unconditional ) { stopTimer(); } private void startTimer() { TimerTask addCount = new TimerTask() { public void run() { textBox.setString( "" + count++); if (count == 10) notifyPaused(); else if (count > 15) resumeRequest(); } }; timer = new Timer(); timer.schedule(addCount, 2000, 1000); } private void stopTimer() { if (timer != null) timer.cancel(); } }
CS 683 Fall 04 Doc 19, MIDlet UI Slide # 13
Based on example, pages 99-100, J2ME in a Nutshell
import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; public class CommandTextBoxMIDlet extends TextBoxMIDlet implements CommandListener { private static final Command EXIT_COMMAND = new Command("Exit", Command.EXIT, 0); private static final Command OK_COMMAND = new Command("OK", Command.OK, 0); private static final Command CLEAR_COMMAND = new Command("Clear", Command.SCREEN, 1); private static final Command REVERSE_COMMAND = new Command("Reverse", Command.SCREEN, 1); public CommandTextBoxMIDlet() { super(); textBox.addCommand(OK_COMMAND); textBox.addCommand(EXIT_COMMAND); textBox.addCommand(CLEAR_COMMAND); textBox.addCommand(REVERSE_COMMAND); textBox.setCommandListener(this); }
CS 683 Fall 04 | Doc 19, MIDlet UI Slide # 14 |
public void commandAction(Command c, Displayable d) { if (c == EXIT_COMMAND) { destroyApp(true); notifyDestroyed(); } else if (c == OK_COMMAND) { textBox.setString("OK pressed"); } else if (c == CLEAR_COMMAND) { textBox.setString(null); } else if (c == REVERSE_COMMAND) { String str = textBox.getString(); if (str != null) { StringBuffer sb = new StringBuffer(str); textBox.setString(sb.reverse().toString()); } } } }
Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.