JAVA ME<br />LWUIT, STORAGE & CONNECTIONS<br />Fafadia-Tech<br />PrasanjitDey<br />prasanjit@fafadia-tech.com<br />
LWUIT<br />The Lightweight User Interface Toolkit (LWUIT) is a versatile and compact API for creating attractive applicati...
Key Features<br />Layouts Manager<br />Pluggable Look and Feel & Themes<br />Fonts<br />Touch Screen<br />Animations & Tra...
Simple Lwuit Program<br />import javax.microedition.midlet.*;<br />import com.sun.lwuit.*; // imports LWUIT<br />import co...
Continued<br />	Form f = new Form("Hello, LWUIT!");<br />f.addComponent("Center",new Label("Prasanjit's j2me 	apps "));<br...
Output<br />Download the LWUIT zip from http://sun.java.com/javame/technology/lwuit and add it to your project /resources ...
Simple Layout Example<br />import javax.microedition.midlet.*;<br />import com.sun.lwuit.layouts.BoxLayout;<br />import co...
Continued<br />	// adding 5 buttons on the form along with a command<br />	Container buttonBar = new Container(new BoxLayo...
Simple Event Handling<br />import javax.microedition.midlet.*;<br />import java.io.IOException;<br />import com.sun.lwuit....
Continued<br />	Form = new Form("Event handling");<br />    	l1 = new Label(" ");<br />    	b1 = new Button("Change label ...
Storage<br />Record Management System or rms is used to provide the storage capabilities in Java ME<br />It stores records...
rms Example<br />import java.io.*;<br />import javax.microedition.midlet.*;<br />import javax.microedition.rms.*; // impor...
Continued<br />	catch(Exception e) {	<br />System.out.println(e);<br />	}<br />	}<br />	public void closeStore() {<br />	t...
Continued<br />	public void getRecord() {<br />	try {<br />		byte getData[]= rs.getRecord(id); // gets the record<br />Sys...
Connections<br />All the important classes and methods for connecting the to the wireless network are included in the java...
Http Example<br />In this program, we create a form and add a command<br />to it. On clicking the command, the data from t...
Continued<br />conn = (HttpConnection)Connector.open(url);<br />conn.setRequestMethod(HttpConnection.GET);<br />conn.setRe...
Continued<br />	finally {<br />	try {<br />		if(is!= null)<br />is.close();<br />		if(conn != null)<br />conn.close();<br ...
Thank You<br />
Upcoming SlideShare
Loading in …5
×

J2ME Lwuit, Storage & Connections (Ft Prasanjit Dey)

8,131 views

Published on

LWUIT, Storage and Connection example of J2ME. We walk through http client method that we use.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
8,131
On SlideShare
0
From Embeds
0
Number of Embeds
70
Actions
Shares
0
Downloads
229
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

J2ME Lwuit, Storage & Connections (Ft Prasanjit Dey)

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

×