TextBox:
When selected, the MIDlet below will display a text box on the screen and will invite users to delete the text present (either using the CLEAR button on the phone or selecting the CLEAR option on the menu using the multidirectional keys and the SELECT central key) and 'type in' via the keyboard on this emulation or by using the standard 'SMS texting' approach, their own text. When users select the GO option from the menu, their text will appear as a moving 'ticker' on the screen, that is, an area of continuously scrolling text. The text on the ticker can be changed as often as the user wishes. The user is also able to stop the MIDlet at any time by pressing the key associated with the STOP command.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TextBoxExample extends MIDlet implements CommandListener
{
private TextBox textbox;
private Display display;
private static final int screenLength = 100;
// commands
private static final Command STOP = new Command("Stop", Command.EXIT, 0);
private static final Command CLEAR = new Command("Clear Screen", Command.SCREEN, 1);
private static final Command GO = new Command("GO", Command.SCREEN, 1);
public TextBoxExample ()
{
public void startApp ()
{
textbox = new TextBox("TextBoxExample", "Delete this text and enter your own, then select option GO", screenLength, TextField.ANY);
// add commands textbox.addCommand(STOP);
textbox.addCommand(CLEAR);
textbox.addCommand(GO);
textbox.setCommandListener(this);
// display the screen
display = Display.getDisplay(this);
display.setCurrent(textbox);
}
public void pauseApp ()
{
}
public void destroyApp (boolean b)
{
}
// command Actions
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);
}
}
}
When selected, the above MIDlet presents the user with an initial screen as shown in Figure.
![608_text box.png](https://www.expertsmind.com/../CMSImages/608_text box.png)
Figure: Initial screen for TextBoxExample MIDlet
Once the screen has been cleared and new text entered, then selecting the GO option from the menu will give a ticker display as shown in Figure.
![1204_text box1.png](https://www.expertsmind.com/CMSImages/1204_text%20box1.png)
Figure: Ticker display with entered text
This MIDlet still has a very simple structure. The class TextBoxExample extends MIDlet as before and now implements CommandListener, this will allow the user to interact with the MIDlet. We shall explain the details of this in a moment.
The general structure is as follows: variable and final declarations (three of which are to do with user interactions), an empty constructor (which could be omitted), then the three standard methods of a MIDlet - startApp, pauseApp and destroyApp - and then the commandAction method.
Once again, the main code is in the startApp method. The line textbox = new TextBox("TextBoxExample","Delete this text and enter your own, then select option GO", screenLength, TextField.ANY); instantiates a TextBox with the title TextBoxExample and with initial contents "Delete this text ... option GO". The variable screenLength determines the maximum number of characters that the text box can hold. A value has to be given and if set to a value greater than the actual screen can hold, then the user will be given the facility to scroll up and down the screen in some device-dependent way - usually by means of the multidirectional key. This value determines the maximum number of characters that the user can enter and anything in excess of this will be ignored. The final argument allows any form of text to be entered. Constraints can be placed on the type of input allowed and data will be checked by the system to ensure it fits the given pattern. For example, TextField.NUMERIC restricts the input to numbers.
The final two lines of startApp display the text box as has been explained before:
display = Display.getDisplay(this);
display.setCurrent(textbox);
With this code, we have a text box that will be displayed on screen and with which users can interact - they can add text (up to a maximum of 100 characters) and they can delete text. However, nothing will happen beyond this. We need to give users the ability to cause code to run as a result of the changes that they have made. To do this, we need to look at handling commands.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your TextBox homework and assignments? Live TextBox experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer TextBox homework help, java assignment help and TextBox 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