Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: Device programming with MIDP, Part 2 - JavaWorld March 2001

Device programming with MIDP, Part 2 - JavaWorld March 2001

Tutorial Details:

Device programming with MIDP, Part 2
Device programming with MIDP, Part 2
By: By Michael Cymerman
Use these user-interface and data-store components to create MIDP-based applications
art 1 of this series was focused on the deployment of the J2MEWTK environment and a rudimentary exploration of the MIDP APIs. This part of the series will focus on the development of an application using the pre-canned user interface components provided in the MIDP API. In addition, I'll explore the conversion and storage of application data to the MIDlet RecordStore . These two concepts will be discussed in detail through a simple Stock Portfolio management application constructed specifically for this article.
Device programming with MIDP: Read the whole series!
Part 1. Build devices with MIDP APIs and J2ME across multiple wireless platforms.
Part 2. Use these user-interface and data-store components to create MIDP-based applications.
Part 3. Use MIDP's communication APIs to interact with external systems.
The hierarchy of Displayable objects
In the examples found in Part 1, I discussed the two major categories of Displayable s: Canvas and Screen .
Canvas is a type of Displayable in which the developer accepts responsibility for creating the entire user interface. This is an extremely useful interface for creating complex graphical interfaces, such as those used in video games. If you choose this path, the UI is drawn on a Canvas object similar to the AWT Canvas used in applets.
Screen is a type of Displayable in which you use predefined components to assemble a user interface. The components are similar to those AWT components used in constructing applets, such as Label and TextField . If you choose this path, you will need to add components to a subclass of the abstract Screen object when necessary to construct your user interface.
I will focus on the Screen type of Displayable object in this part of the series, since the Canvas object was sufficiently discussed Part 1. As I mentioned above, the Screen object is an abstract class of Displayable . The subclasses of the Screen class are Alert , Form , List , and TextBox .
In this article, I will examine the mechanics of these Displayable objects, including the construction, interaction, and event-handling schemes that enable them to come together to form an application.
Detailed example
The best way to learn how to use some of these screens is through an example. The example used here contains some workflow that demonstrates the following concepts:
Construction with different screen types
Construction with different screen components
Usage of commands from within the javax.microedition.lcdui classes
An implementation of MVC for efficient screen manipulation
Interaction with the javax.microedition.rms data store on the device
To provide this information, I will use a simple rendition of a brokerage application. In this application, the user can buy a stock or sell a stock that he or she currently owns. Please understand that this process has been highly simplified in order to demonstrate key concepts.
In Part 3 of this series, I will expand this application to connect with a ticker service to retrieve accurate pricing for the purchase-using HTTP.
ExampleMidlet
The ExampleMidlet provides the context for this application. It controls the flow and provides access to the device resources as defined in the MIDP specification.
public class ExampleMidlet extends MIDlet implements Controller
As shown, the ExampleMidlet implements the Controller interface to manage the screen interactions. The intent of the Controller interface is to provide the services needed by both the view and model classes.
Controller interface
The Controller interface contains methods to access the StockDatabase , which is a simple RMS data store, and to manipulate the Display object associated with the MIDlet. The Controller interface in this example is a simple interface that you can expand to include methods to retrieve properties, make HTTP connections, or whatever your application requirements deem necessary.
public interface Controller
{
public StockDatabase getStockDatabase();
public Displayable currentScreen();
public void nextScreen(Displayable display);
public void lastScreen();
}
By implementing the Controller interface, the ExampleMidlet can expose certain methods to the individual screens. You could instead pass the MIDlet as a parameter to the screens, but you probably don't want to interfere with the normal MIDlet lifecycle.
Within the ExampleMidlet , the nextScreen() and lastScreen() methods have been used to maintain the information about the currently visible screen. These methods use the java.util.Stack object to maintain the display state.
The application will push the currently displayed Displayable object to the Stack prior to displaying the next screen when nextScreen is called.
public void nextScreen(Displayable display){
Displayable currentScreen = this.getDisplay().getCurrent();
if ( currentScreen != null)
_screenStack.push( currentScreen );
getDisplay().setCurrent( display);
}
The application will pop the previously displayed Displayable object from the Stack and set the display to show that screen.
public void lastScreen()
{
Displayable display = null;
if ( !_screenStack.empty() )
display = (Displayable) _screenStack.pop();
else
display = new WelcomeScreen( (Controller) this );
getDisplay().setCurrent( display);
}
There are other ways in which you could handle the management of the screens. For example, you could read in a screen from the properties file and use that list to manage the interactions between the display screens. This approach provides the necessary management for this example without suffering through a more complex approach.
WelcomeScreen
The first screen that is visible to the user is the WelcomeScreen . Let's walk through the code for this screen. The WelcomeScreen extends from the javax.microedition.lcdui.List object and implements the CommandListener interface.
public class WelcomeScreen extends List implements CommandListener
The constructor is passed a parameter that contains a reference to the Controller as an argument, which will be stored as a private variable for future use.
public WelcomeScreen(Controller controller)
Because the WelcomeScreen extends the List object, the superclass must be called for instantiation. The parameters passed are the title of the screen and the type of list.
super("Welcome", List.IMPLICIT);
List
In this case, the list is of type IMPLICIT . The list types that you can use are shown in Table 1.
Table 1. List types defined
List Type
Definition
IMPLICIT
Neither checkboxes nor radio buttons are visible next to each item in a list. Users can select only one item from the list. Similar to an HTML Option list.
EXPLICIT
Radio buttons allow users to select only one item from the list.
MULTIPLE
Checkboxes allow users to select multiple items from the list.
Ticker
The WelcomeScreen also contains a ticker that generates a simple welcome message to the user. To create a ticker , call its constructor with a String object of the message to be scrolled and then call the setTicker() method of the Screen object to add the ticker to the heading.
Ticker ticker =
new Ticker("Thank you for viewing the Stock Application Example");
this.setTicker( ticker);
Using the append() method, you can add each item to the list for display.
append("Buy Stock", null);
append("Sell Stock", null);
The WelcomeScreen will listen and handle any commands that are initiated when it is displayed. Therefore, it calls the setCommandListener() method with itself as a parameter.
this.setCommandListener(this);
CommandListener
The event-handling infrastructure in the MIDlet architecture, briefly discussed in the WelcomeScreen example, lets you handle events generated from the MIDlet Screen objects.
Events are generated on user actions, such as clicking the soft-key menu buttons on a Sreen or selecting items from a List . When these events are generated, the commandAction() method of the objects that have registered interest in the current screen will be executed.
For the WelcomeScreen example, the user is presented with a list containing two items: "Buy Stock" and "Select Stock." When the user selects one of the items by using the up/down arrows and then clicking the action button, the commandAction() method will be called.
public void commandAction (Command c, Displayable d)
{
if ( c == List.SELECT_COMMAND)
{
List shown = (List) _controller.currentScreen();
switch (shown.getSelectedIndex())
{
case 0:
_controller.nextScreen( new BuyStockScreen( _controller) );
break;
case 1:
default:
_controller.nextScreen( new SelectStockScreen(_controller) );
}
}
}
This commandAction() method demonstrates how to capture the events generated by an IMPLICIT list. The first step involves retrieving the list from the display, which in this example is done through the Controller interface. The selected item can be retrieved from the list to allow the application to perform logical operations to determine the proper course of action. In this case, the next display object will be instantiated and passed to the controller's nextScreen() method.
BuyStockScreen
Continuing the example, suppose the user has selected the "Buy Stock" option from the Welcome screen. In this section, I will examine the BuyStockScreen object that would be instantiated in that case.
public class BuyStockScreen extends StockScreen implements CommandListener
The BuyStockScreen class extends the StockScreen class, which itself extends the javax.microedition.lcdui.Form class.
StockScreen
The constructor is passed the title of the screen and a reference to the controller object, which is to be stored in a protected variable for use by the child class. The title is passed because the StockScreen object is inherited by the BuyStoc


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Device programming with MIDP, Part 2 - JavaWorld March 2001

View Tutorial:
Device programming with MIDP, Part 2 - JavaWorld March 2001

Related Tutorials:

Displaying 1 - 50 of about 2265 Related Tutorials.

J2ME Books
with the Mobile Information Device Profile (MIDP). With labs. Published... Information Device Profile (MIDP), which this text centers on, plus the Connected... to traditional Java 2 programming.        
 
Programming: Initials 2
Java: Programming: Initials 2 Java: Programming: Initials 2 Name... extra can cancel out points you may lose in the main part of the problem
 
Struts 2 Tutorials for Beginners, Struts 2 Tutorial
Struts 2 Tutorial,Struts2 Examples,Apache Struts 2 Tutorials - Free Java Programming Tutorials Struts 2 Tutorial  ... part of any web application. With the release of Struts 2, validation are now much
 
Programming Style Guideline
Java Notes: Programming Style Guideline Java Notes: Programming Style Guideline Contents I. Motivation for programming guidelines II. Comments, indentation, spacing, braces, ... III
 
Struts 2 Tutorial
Struts 2 Tutorial,Struts2 Examples,Apache Struts 2 Tutorials - Free Java Programming Tutorials Struts 2 Tutorial  ... part of any web application. With the release of Struts 2, validation are now much
 
Wi-Fi as a part of LBS
Wi-Fi as a part of LBS Wi-Fi as a part of LBS            ... the attached Wi-Fi device again converts it into binary code for the respective
 
JDO UNPLUGGED - PART 1
UNPLUGGED - PART I JDO UNPLUGGED - PART I... , in the next part of this tutorial...; SPD Oreilly Publication. 2. Java Data Objects (2003)    - Robin M
 
JDO UNPLUGGED - PART II
UNPLUGGED - PART II JDO UNPLUGGED - PART II... interfaces and classes defined in the JDO specification. 2. jdori.jar.../products/jta/index.html 2. antlr.jar : It is the parsing technology used
 
Java Programming Books
;    Advanced Programming for the Java 2 Platform...;    Advanced Programming for the Java 2... Java Programming Books Java Programming Books
 
C/C++ Programming Books
C/C++ Programming Books C/C++ Programming... topics for Visual C++ 6 programming. This book skips the beginning level material... Foundation Class Library. These enhancements include classes for Internet programming
 
Definition of Bioinformatics
; About Bioinformatics In February 2001, the human genome was finally... for Biotechnology Information (NCBI 2001) defines bioinformatics as: "... the Computer Programming Languages.    Experiment on your computer
 
Java Programming Idioms
Java: Java Programming Idioms Java: Java Programming Idioms Introduction Every programming language has its ways of writing common programming constructions. Sometimes
 
Net caboodle plugin
;  The Business 2 Mobile plugin from Net Caboodle makes it easy for you to develop MIDP clients for your business services, without the need to write any of your own protocol code. The plugin generates MIDP 'stubs' for your
 
Programming Style Guideline
tabs by blanks. Use it if you have it. 2. Programming... Java Notes: Programming Style Guideline Java Notes: Programming Style Guideline
 
J2EE Interview Questions -2
Questions -2           ... and enterprise information systems (EIS) as part of enterprise application integration (EAI.... EJB is used for server side programming whereas java bean is a client side. Bean
 
Programming: Hammurabi I - Solution
Java: Programming: Hammurabi I - Solution... The following two source files are a solution to the Hammurabi I programming... program 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
 
Java Interview Questions - Page 2
Interview Questions - Page 2    ...: 1. The Java Virtual Machine (Java VM) 2. The Java Application Programming Interface (Java API) Question: What
 
C# Programming Books
C# Programming Books C# Programming Books... rights reserved. No part of this publication may be reproduced, stored... With Microsoft's introduction of the .NET platform, a new, exciting programming language
 
New to programming...
Java Programing, New to Java Programming, New in Java Programming... to programming...  ... & D's, the real life programming is here....having no space for complexities
 
JSP Programming Books
JSP Programming Books JSP Programming Books... The Java 2 Platform has become the technology of choice for developing... architectures. Although JSP is a key component of the Java 2 Platform
 
SCADA Programming
SCADA Programming SCADA Programming... technology was developed as part of Instrumentation Engineering. Monitoring systems... are recorded and displayed. Where Programming Comes In All of this requires
 
Maven 2 Eclipse Plug-in
Maven 2 Eclipse Plug-in Maven 2 Eclipse Plug... of programmers; it actually reduces the repetitive tasks involved in the programming... from   http://www.eclipse.org/downloads/ 2. Get Maven-eclipse-plugin
 
Programming
Java: Programming Java NotesProgramming Here are some tips on making programming student problems... elements of Extreme Programming (a much hyped, but good Software
 
JSP Session Counter Using SessionListener
;        Counter is a device which... of the HttpSessionListener interface.This listener is a part of HttpSession for the HttpSession object...; <html> <head> <title>Page 2</title> <
 
Programming - World Peace
Java: Exercise - Programming - World Peace Java: Programming - World Peace Change all occurences... to extract the part before "war" and after "war" and build a new string
 
Programming - WordFrequency modifications
Java Notes: Programming - WordFrequency modifications Java Notes: Programming - WordFrequency..... After you have the previous part running, change the Comparator
 
Programming: Weeks and Days
Java: Programming: Weeks and Days... and output values Input: 9 Output: 1 week(s), 2 day(s) Input: 20 Output: 2 week(s), 6 day(s) Input: 1 Output: 0 week(s), 1 day(s
 
Programming - Transform Name
Java: Exercise - Programming - Transform Name Java: Programming - Transform Name Write a program... Output: Madonna Case 2 - Change First Last into Last, First
 
Programming: Prime Numbers - Dialog
Java: Programming: Prime Numbers - Dialog... numbers are 2, 3, 5, 7, 11, 13, 17, ... Prime numbers have many interesting... to decide if n is prime or not is to try dividing it by all integers from 2
 
AN INTRODUCTION TO JSTL
.   Tutorial Home | Part 1 | Part 2Part 3 | Part 4.... Tutorial Home | Part 1 | Part 2Part 3 | Part 4  ... on this article may be sent to:  ramrsr@rediffmail.com ) In this four-part
 
Programming: Count Words - Dialog
to go about it. 1. Read a string from the user. 2. Initialize the blank... iterative programming to make it easier Start with the minimum program, one
 
Programming - Transform Name - Answer
Java: Exercise - Programming - Transform Name - Answer Java: Programming - Transform Name - Answer..."), and the case where the names must be rearranged. Iterative programming Here
 
Open Source MP3 Player
the device's hardware schematics later this month. We're willing to support... programming team was cut loose. It was allowed to keep all rights to its work, a provision that was part of its original deal in joining iCast, the programmers say
 
Programming: Hammurabi I
Java: Programming: Hammurabi I... requires 2 * area bushels of grain. Harvest. There are variations in the weather each year. The yield varies from 2 to 6 bushels per planted acre
 
Programming: Hammurabi I
Java: Programming: Hammurabi I... requires 2 * area bushels of grain. Harvest. There are variations in the weather each year. The yield varies from 2 to 6 bushels per planted acre
 
Why Struts 2
New Features of Struts,Features of struts 2,Why Struts 2 Why Struts 2          ... announcement, some key features are: Simplified Design - Programming the abstract
 
Why Struts 2
New Features of Struts,Features of struts 2,Why Struts 2 Why Struts 2          ... announcement, some key features are: Simplified Design - Programming the abstract
 
SME Server 7.0 Pre 2 has been released now
SME Server 7.0 Pre 2 has been released now SME Server 7.0 Pre 2 has been released now SME Server 7.0 pre-release 2... a solid, easy-to-use server for their small-business customers. In July 2001, e-smith
 
Text Clock 2
Java: Example - Text Clock 2 Java: Example - Text Clock 2 This is the same as Example - Text Clock... for an explanation of a simple timer class. Main program / Applet 1 2 3 4
 
Technology What is and FAQs
it is the part of the MPEG-4 standard.    ASCIIASCII, the abbreviation... programming package that can be access on  a single firm operating... is a collection of Java Programming Language API (Application programming
 
Programming: Hammurabi I - Project start
Java: Programming: Hammurabi I - Project start Java: Programming: Hammurabi I - Project start Copy... programming, here is a working version, but it doesn't implement all of the features
 
New to Java?
Information Device Profile (MIDP) together provides solid Java platform... Information Device Profile (MIDP) - This is another configuration of Java Micro Edition... an overview of Java technology as programming language and a platform. Java
 
New to Java?
Information Device Profile (MIDP) together provides solid Java platform... Information Device Profile (MIDP) - This is another configuration of Java Micro Edition... an overview of Java technology as programming language and a platform. Java
 
Java: String Exercise 2
Java: String Exercise 2 Java: String Exercise 2 Name ______________________ Assume the following.... 1__________h.length() 2__________h.substring(1) 3__________h.toUpperCase() 4
 
Where is Java being Used?
Where Java is Used,Uses of Java Programming,Java Languages Uses...;         The programming... as one of the platform independent programming language. The most important feature
 
DSL Filter
part of DSL service that requires sometimes for installing the DSL device. DSL filter, also known as micro filter is an analog low-pass filter device that installs on telephone and other analog device for preventing interference between
 
Struts 2 Training
Struts 2 Training, Training for developing applications with Struts 2 Struts 2 Training       ...;       The Struts 2 Training for developing
 
Difficult Interview Questions Page -2
Difficult Interview Questions Page -2 Difficult Interview Questions Page -2       ... if you had the part of any big project, give the example what program
 
Misc.Online Books
programmer is difficult and noble. The hardest part of making real a collective... not assume knowledge of any particular programming languages, standards, or protocols...; Programming Languages  This book is an introduction
 
What is Java, it?s history?
What is Java,Java Programming History,Definition of Java,Java Language...; Java is a high-level object-oriented programming language developed... the consumer electronics and communication equipments. It came into existence as a part
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.