1. JAVA ME LWUIT, STORAGE & CONNECTIONS Fafadia-Tech PrasanjitDey prasanjit@fafadia-tech.com
2. LWUIT The Lightweight User Interface Toolkit (LWUIT) is a versatile and compact API for creating attractive application user interfaces for mobile devices LWUIT provides sophisticated Swing-like capabilities without the tremendous power and complexity of Swing LWUIT offers a basic set of components, flexible layouts, style and theming, animated screen transitions, and a simple and useful event-handling mechanism LWUIT is developed by Sun Microsystems and is inspired by Swing
3. Key Features Layouts Manager Pluggable Look and Feel & Themes Fonts Touch Screen Animations & Transitions 3D and SVG Graphics Integration Tools Bi-directional text support
4. Simple Lwuit Program import javax.microedition.midlet.*; import com.sun.lwuit.*; // imports LWUIT import com.sun.lwuit.layouts.BorderLayout; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; import java.io.IOException; public class Hello extends MIDlet implements ActionListener { public void startApp() { Display.init(this); // initializes the display try { // using a theme here Resources r = Resources.open("/res/ThemeJava1.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava1")); } catch (IOExceptionioe) { // Do something here. }
5. Continued Form f = new Form("Hello, LWUIT!"); f.addComponent("Center",new Label("Prasanjit's j2me apps ")); f.show(); // display the form Command exitCommand = new Command("Exit"); f.addCommand(exitCommand); f.setCommandListener(this); } public void pauseApp() { } public void destroyApp(boolean unconditional) {} public void actionPerformed(ActionEventae) { notifyDestroyed(); } }
6. Output Download the LWUIT zip from http://sun.java.com/javame/technology/lwuit and add it to your project /resources directory, also add all your themes, images and other resources in a folder, zip it and add to your project/resources directory.
7. Simple Layout Example import javax.microedition.midlet.*; import com.sun.lwuit.layouts.BoxLayout; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; import com.sun.lwuit.events.ActionListener; import com.sun.lwuit.events.ActionEvent; import java.io.IOException; public class Layouts extends MIDlet implements ActionListener { Form form; Command exit; public void startApp() { Display.init(this); try { Resources r = Resources.open("/res1/ThemeJava2.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava2")); } catch(IOExceptionioe) { System.out.println(ioe); } form = new Form("Layouts ");
8. Continued // adding 5 buttons on the form along with a command Container buttonBar = new Container(new BoxLayout(BoxLayout.X_AXIS)); buttonBar.addComponent(new Button("Add")); buttonBar.addComponent(new Button("Remove")); buttonBar.addComponent(new Button("Edit")); buttonBar.addComponent(new Button("Send")); buttonBar.addComponent(new Button("Exit")); exit = new Command("Exit"); form.addComponent(buttonBar); // buttonBar is a container for other buttons form.addCommand(exit); form.setCommandListener(this); form.show(); public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void actionPerformed(ActionEventae) { notifyDestroyed(); } }
9. Simple Event Handling import javax.microedition.midlet.*; import java.io.IOException; import com.sun.lwuit.*; import com.sun.lwuit.events.*; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; public class EventHandling extends MIDlet implements ActionListener { Form form; int c = 1000; Label l1; Button b1,b2; public void startApp() { Display.init(this); try { Resources r = Resources.open("/res1/ThemeJava2.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava2")); } catch(IOExceptionioe) { System.out.println(ioe); }
10. Continued Form = new Form("Event handling"); l1 = new Label(" "); b1 = new Button("Change label to add "); form.addComponent(b1); form.addComponent(l1); form.show(); b1.addActionListener(this); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void actionPerformed(ActionEventae) { c++; l1.setText("" +c); form.layoutContainer(); } }
11. Storage Record Management System or rms is used to provide the storage capabilities in Java ME It stores records in binary format inside the Record Stores Data is not lost even if the device is switched off rms provides various methods for storing and retrieving records: openRecordStore() closeRecordStore() deleteRecordStore() getRecord() enumerateRecords()
12. rms Example import java.io.*; import javax.microedition.midlet.*; import javax.microedition.rms.*; // imports all rms resources public class rmsDemo extends MIDlet { intsize_available, id; RecordStorers; public rmsDemo() { openStore(); // open recordstore addRecord(); // adds record into recordstore getRecord(); // gets all rcords closeStore(); // closes the recordstore } public void startApp() {} public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void openStore() { try { rs=RecordStore.openRecordStore("names",true); size_available=rs.getSizeAvailable(); System.out.println("Start"); System.out.println("Available size is " +size_available); }
13. Continued catch(Exception e) { System.out.println(e); } } public void closeStore() { try { rs.closeRecordStore(); } catch(Exception e) { System.out.println(e); } } public void addRecord() { try { String record="java ME persistent storage"; byte data[]=record.getBytes(); // converts into bytes of data id=rs.addRecord(data,0,data.length); } catch(Exception e) { System.out.println(e); } }
14. Continued public void getRecord() { try { byte getData[]= rs.getRecord(id); // gets the record System.out.print("Records in byte format: "); for(int j=0;j<getData.length;j++) { System.out.print(getData[j]); System.out.print(" "); } System.out.println(); System.out.print("Records in string format: "); for(inti=0;i<getData.length;i++) { System.out.print(new String(new byte[]{getData[i]})); } System.out.println(); System.out.println("Done with it"); } catch(Exception e) { System.out.println(e); } } }
15. Connections All the important classes and methods for connecting the to the wireless network are included in the javax.microedition.io.* package The connections types are provided by the InputStream and the OutputStream interfaces These interfaces adds the ability to input and output data over the network There are three important level of connections available Socket Datagram HTTP connection
16. Http Example In this program, we create a form and add a command to it. On clicking the command, the data from the url is displayed on the device. Here is the actual thread to display the data on the device. public void commandAction(Command c, Displayable d) { if(c==exit) { destroyApp(true); notifyDestroyed(); } else { new Thread(new Runnable() { public void run() { try { HttpConnectionconn = null; String url = "http://www.burrp.com/robots.txt"; InputStream is = null; try {
17. Continued conn = (HttpConnection)Connector.open(url); conn.setRequestMethod(HttpConnection.GET); conn.setRequestProperty("User-Agent","Profile/MIDP-2.1 Confirguration/CLDC-1.1"); intrespCode = conn.getResponseCode(); if (respCode == conn.HTTP_OK) { StringBuffersb = new StringBuffer(); is = conn.openDataInputStream(); intchr; while ((chr = is.read()) != -1) sb.append((char) chr); form.append("Here is the records from www.burrp.com: " + sb.toString()); } else { System.out.println("Error in opening HTTP Connection. Error#" + respCode); } } catch(Exception e) { System.out.println(e); }