MIT AITI Mobile Application Development in Java Lab 15: J2ME Introduction
MIT AITI Mobile Application Development in Java Lab 15: J2ME Introduction
Documentation
AssumingyourWTKislocatedatC:\Java_ME_platform_SDK_3.0,youcanfindtheJ2MEdocumentation at:C:\Java_ME_platform_SDK_3.0\docs.TheMIDPdocumentationisrootedat: C:\Java_ME_platform_SDK_3.0\docs\api\midp2.0\index.html. MITAfricaInformationTechnologyInitiative 1
Figure1:FirstScreen
Figure2:SecondScreen
MITAfricaInformationTechnologyInitiative
2. Inyourconstructor,initializealltheobjectsyouwillneed(oneChoiceGroup,two TextFields,oneForm,oneTextBox,twoCommands,andoneDisplayobject).Hereare somesampleconstructions: queryCommand = new Command("[command name]",[Command type],0); //dont worry too much about the last two arguments //Command.EXIT or Command.OK are two options for Command types display = Display.getDisplay(this); submitter = new TextField("[title]","[initial text]", [max length],TextField.ANY); //TextField.ANY means any characters can be typed theItems = new ChoiceGroup("[title]",ChoiceGroup.EXCLUSIVE); //EXCLUSIVE means only one option may be selected at once theItems.append("Nairobi, Kenya",null); //The null refers to the image for the entry, but we wont have //any images results = new TextBox("[title]","[initial text]", [max length],TextField.ANY); 3. UsetheaddCommand()andappend()methodstocompletethesetup.The ChoiceGroupandTextFieldsmustbeappend()edtotheForminordertobe displayed.Wellgetintothatmorelater,butessentiallyChoiceGroupandTextFieldare setuptobedisplayedinaformalongwithmanyotherItems. BothCommandsshouldalsobeaddedtotheForm.Theexitcommandshouldalsobeaddedto theTextBox. 4. HaveyourMIDletimplementtheCommandListenerinterface.Thismeansthatitshould haveavoid commandAction(Command [variable name],Displayable [variable name])method. UsesetCommandListener(this)tosetthecurrentinstanceofyourMIDletasthe commandlistenerforyourFormandTextBox.Thismeansthatwheneveracommandis calledfromeitheroftheseobjects,thecommandAction()methodofyourMIDletwillbe called. WewillfillinthecommandAction()methodinstep6. 5. InthestartApp()method,useTextFieldssetString()toresettheTextFieldss initialtextblank.ThenuseDisplayssetCurrent()methodtodisplaytheForm.
MITAfricaInformationTechnologyInitiative
6. NowwewillwritethecommandAction()methodofourMIDlet.IftheCommandargument isyourexitCommand,quittheprogram.DothisbycallingdestroyApp(true) (unnecessaryhere,butgoodstyle)andthennotifyDestroyed()(actuallyquitsthe MIDlet). IftheCommandisyourqueryCommand,therewillbetwocases: a) Thefirstcaseiswherenocityhasbeenselectedfromthelist.Determinethisusing ChoiceGroupsgetSelectedIndex()method.Itshouldreturn1ifnothingis selected. Inthiscase,callyourgetWeather()functionwiththequery[cityname]+,+[country name].Savethisasyourresult.UseTextFieldsgetString()methodtofindout whattheuserhastypedintotheTextFields. b) Thesecondcaseiswhereacityhasbeenselectedfromthelist.UseChoiceGroups getString(int)todeterminewhichcitywasselected.CallyourgetWeather() methodonthisStringandsavetheoutputasyourresult. 7. ChangetheTextBoxsvaluetoyourresultwiththesetString()method.Thenchange yourDisplay(usesetCurrent())totheTextBox. 8. Runyourprogramtotestit.IfthisisyourfirstJ2MEapplication,feelproud.
MITAfricaInformationTechnologyInitiative