Adding interaction Assignment Help

Assignment Help: >> High level interfaces >> Adding interaction

Adding interaction:

The treatment of commands in J2ME is quite similar to the treatment of events in J2SE.

  • A command (rather than event) listener class is written, which defines some behaviour to be executed if the user activates a particular command.
  • A listener object is created and registered with the relevant display object.
  • When the system detects that the user has activated the command, the appropriate method, such as commandAction, is automatically invoked on the listener object.

One difference is that in J2ME we define the range of commands available to the user, whereas in the AWT event model the possible events are predefined. When a method such as commandAction is invoked, the command is passed as an argument and used to determine what action is to be taken.

In order to use a command listener, we proceed as follows.

Step 1 Defining the command options
To start we need to define the command options that we wish the user to have. The display of these command options is device dependent but there is some limited scope for the developer to infiuence the display.

In the example, we have three commands, STOP, CLEAR and GO. To define STOP, for instance, we use the statement:

private static final Command STOP = new Command("Stop", Command.EXIT, 0);

Mobile devices usually do not have sufficient screen space to display buttons and menu bars for individual options, and so the device will make decisions as to how the options are to be presented to the user.

The Command constructor has the following three arguments.

  • The first argument gives the command a label, which the device will attempt to display in some way.
  • The second argument gives the type of the command.
  • The third argument sets the priority. This allows the developer to indicate to the device the relative importance of the command.

To see how the priority is used, consider a mobile phone. Typically, this will have two 'soft buttons', one placed underneath the right-hand side of the screen and the other under the left-hand side of the screen. These buttons then respond to the command displayed on the screen directly above them.

If there are more than two commands - as in our example - then on a mobile phone, each command cannot be allocated its own soft key. In that case, the system running on the device will have to make a decision on how the commands will be displayed.

You can see the outcome of such a decision in Figure. On the left-hand side we have Stop and on the right-hand side Menu. Three commands were too many for the device to display with two soft keys, and so two were placed in a menu and the other was displayed on the left-hand side.

In general, the effect of a priority value is that the lower it is, the more likely a command is to get a soft key of its own. However, the outcome is also dependent on the type of command; command types such as Command.EXIT and Command.BACK tend to be favoured over more general commands such as Command.SCREEN. This is the reason why in our example STOP, of type EXIT, was assigned its own soft key.

Step 2 Adding commands to a Displayable object
To add the commands to the text box we use the addCommand method. In our example, the commands are added by the three statements:

textbox.addCommand(STOP); textbox.addCommand(CLEAR); textbox.addCommand(GO);

Step 3 Registering a CommandListener with the relevant Displayable object
In our example, we need to register a CommandListener to a TextBox. This is done by invoking the setCommandListener method.

Step 4 Defining a CommandListener class
We need to define a class that will implement the CommandListener interface, but override the commandAction method to provide the required behaviour. Method commandAction is passed two parameters: one is the command invoked by the user and the other is a reference to the displayable interface with which it is associated. With the aid of this information it can update the interface to refiect the result of the user's action.

In our example, we have declared our MIDlet as implementing commandListener and so made it possible for the MIDlet itself to be registered as a listener of the objects in its own display. This is a very common technique, also often used with applets and other GUI displays.

Since the MIDlet implements commandListener it must implement the commandAction method that belongs to that interface, as follows:

public void commandAction (Command c, Displayable d)
{
if (c == GO)
{
String text = textbox.getString( );
// create the Ticker
Ticker ticker = new Ticker(text);
textbox.setTicker(ticker);
}
if (c == STOP)

{
destroyApp(true);
notifyDestroyed( );
}
if (c == CLEAR)
{
textbox.setString(null);
}
}

From this, it should be fairly clear what effect it has.

  • GO gets the current contents of the text box and instantiates and installs a ticker on the text box.
  • STOP invokes the destroyApp method, which arranges for a safe termination of resources and then notifyDestroyed tells the platform that this MIDlet can be terminated.
  • CLEAR sets the text box to the null string. 

The ticker can be seen in Figure, at the top of the screen and above the text box label. The placing and scrolling speed of a ticker is system dependent.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Adding interaction homework and assignments? Live Adding interaction experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Adding interaction homework help, java assignment help and Adding interaction projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd