Form:
The second subclass of Screen that we will look at is the Form class. This class provides an easy way to construct user interfaces from a collection of items, such as TextField, StringItem and Gauges (a gauge is in effect a progress bar, however, the actual representation of it is device dependent). Commands can also be associated with these interfaces as they were with TextBox. All Items are subclasses of the abstract class Item. The class Form has the following two constructors, the first creates an empty Form with a title and the second creates a Form with a title and a set of Items held in an array.
public Form(String title);
public Form(String title, Item [] items);
Items can be added to a Form using one of three append methods:
public void append(Item item);
public void append(Image image);
public void append(String string);
Again, we will use an illustrative MIDlet to focus on some key features of the Form and Item classes. The following MIDlet uses a Form to display a text field to users inviting them to enter some text up to a total of 20 characters. As the text is entered, the gauge below the text field indicates how much of the text field space has been used. This simple program is used to demonstrate an alternative form of interaction with a Screen object using the ItemStateListener interface.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class FormExample extends MIDlet implements ItemStateListener
{
private Form form;
private Display display;
private TextField textfield;
private Gauge gauge;
public FormExample ()
{
public void startApp ()
{
form = new Form("FormExample");
// create Text Field and add to Form
textfield = new TextField("Input some number of characters up to 20", null, 20, TextField.ANY);
form.append(textfield);
// create Gauge and add to Form
gauge = new Gauge("Number of characters input", false, 20, 0);
form.append(gauge);
// add ItemStateListener
form.setItemStateListener(this);
// display form
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp ()
{
}
public void destroyApp (boolean b)
{
}
// add code when text field changes
public void itemStateChanged (Item it)
{
if (it instanceof TextField)
{
int number = textfield.getString().length();
gauge.setValue(number);
}
}
}
Figure shows the result of the above program.
Figure: The initial screen for FormExample
After some text has been added the screen, as shown in Figure, is visible.
Figure: FormExample after text has been entered
This program makes use of the ItemStateListener, which is similar to the CommandListener (which can also be used on Forms) that we met earlier. As its name suggests, it listens for changes in the state of any of the Items appended to a Form. The major difference between the two types of listeners is that the ItemStateListener will respond as soon as a change is detected and will not wait for a command option to be selected by the user. This type of response can be more useful for some types of interfaces.
The listener is registered with the MIDlet as follows:
form.setItemStateListener(this);
and it has one method which has to be implemented:
public void itemStateChanged(Item it)
{
if (it instanceof TextField)
{
int number = textfield.getString( ).length( );
gauge.setValue(number);
}
}
In this case, we are only looking at state changes to one of the items - the TextField. If the item that has changed is an instance of a TextField then the current length of the string in the field is found and the gauge is updated. The response time for detecting a state change is device dependent and so may not respond as quickly as you might like.
The gauge itself is created by:
gauge = new Gauge("Number of characters input", false, 20, 0);
The initial string acts as a label, false says that the user cannot interact directly with the gauge, 20 is the maximum value and 0 is the initial value of the gauge. There is no developer control over how the gauge will be displayed; this is entirely device dependent.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Form homework and assignments? Live Form experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Form homework help, java assignment help and Form projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours