×
Namespaces

Variants
Actions
(Difference between revisions)

Creating CustomItem in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
seppo_fn (Talk | contribs)
marcelobarrosalmeida (Talk | contribs)
(Marcelobarrosalmeida - adding screenshot)
(16 intermediate revisions by 6 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:Code Examples]][[Category:UI]][[Category:Code Snippet]][[Category:Symbian]][[Category:Series 40]][[Category:Series 40 3rd Edition FP1]][[Category:S60 3rd Edition (initial release)]][[Category:S60 3rd Edition FP1]][[Category:S60 3rd Edition FP2]][[Category:Series 40 6th Edition FP1]]
__NOEDITSECTION__
+
{{ArticleMetaData <!-- v1.2 -->
{{CodeSnippet
+
|sourcecode= [[Media:Creating custom control in J2ME.zip]]
|id=&nbsp;
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|platform=Series 40 3rd Edition, FP1, S60 3rd Edition, S60 3rd Edition, FP1, S60 3rd Edition, FP2
+
|devices= Nokia 6131, Nokia E70, Nokia N78, Nokia C3-01
|devices=Nokia 6131, Nokia E70, Nokia N78
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|category=Java ME
+
|platform= Series 40, S60 <!-- Series 40 3rd Edition FP1, S60 3rd Edition (initial release), S60 3rd Edition FP1, S60 3rd Edition FP2 -->
|subcategory=UI
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|creationdate=November 5, 2008
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|keywords=javax.microedition.lcdui.CustomItem
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= javax.microedition.lcdui.CustomItem
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20081127
 +
|author= [[User:Vltsoy]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001267
 
}}
 
}}
  
 +
{{ArticleNeedsUpdate|timestamp=20120810095816|user=[[User:TrashedDev|TrashedDev]]|The full potential of this article isn't available on full-touch devices since this article needs to have a physical number pad.}}
  
 
==Overview==
 
==Overview==
  
This code snippet demonstrates how to create and display custom control. It consist of 2 files:
+
This code snippet demonstrates how to create and display custom control. It consist of two files:
 +
# '''CustomControl.java''' - Inherited from {{Icode|MIDlet}}, displays the {{Icode|MyItem}} control.
 +
# '''MyItem.java''' - Inherited from {{Icode|CustomItem}}.
  
1. CustomControl.java - inherited from <tt>MIDlet</tt>, displays MyItem control.
+
Custom contol class {{Icode|MyItem}} implements the method {{Icode|keyPressed()}}, which is responsible for storing the last key-pressed code.
 
+
It also implements the {{Icode|paint()}} method, which displays the last key-pressed code inside the control.
2. MyItem.java       - inherited from <tt>CustomItem</tt>.
+
 
+
Custom contol class <tt>MyItem</tt> implements method <tt>keyPressed()</tt>,which is responsible for storing last key pressed code.
+
Also it implements method paint(), which displays last key pressed code inside the control.
+
  
  
Line 27: Line 40:
  
 
<code java>
 
<code java>
 
 
import javax.microedition.lcdui.Command;
 
import javax.microedition.lcdui.Command;
 
import javax.microedition.lcdui.CommandListener;
 
import javax.microedition.lcdui.CommandListener;
Line 34: Line 46:
 
import javax.microedition.lcdui.Form;
 
import javax.microedition.lcdui.Form;
 
import javax.microedition.midlet.MIDlet;
 
import javax.microedition.midlet.MIDlet;
 
  
  
Line 102: Line 113:
 
     }
 
     }
 
}
 
}
 
 
 
 
</code>
 
</code>
  
Line 110: Line 118:
  
 
<code java>
 
<code java>
 
 
import javax.microedition.lcdui.CustomItem;
 
import javax.microedition.lcdui.CustomItem;
 
import javax.microedition.lcdui.Font;
 
import javax.microedition.lcdui.Font;
Line 188: Line 195:
 
     }
 
     }
 
}
 
}
 
 
</code>
 
</code>
  
 
==Postconditions==
 
==Postconditions==
  
After starting MIDlet, user will see custom control with last key pressed code.
+
After starting the MIDlet, the user will see custom control with the last key-pressed code.
 +
 
 +
[[Image:custom_item.png|center|240px]]
  
 
==Supplementary material==
 
==Supplementary material==
  
You can see source file and executable application in attached zip archive. Archive is available for download at [[Media:Creating_custom_control_in_J2ME.zip]]
+
You can view the source file and executable application in the attached zip archive. The archive is available for download at [[Media:Creating custom control in J2ME.zip]]
  
[[Category:Java ME]][[Category:Code Examples]][[Category:UI]]
+
Related articles:
 +
* [[Archived:LCDUI CustomItem content remains blank when appended to a Form before setting the Form as current Displayable (Known Issue)]]
 +
* [http://www.developer.nokia.com/Resources/Library/Java/developers-guides/ui-and-graphics/lcdui/using-the-lcdui/form/customitem.html CustomItem] (Java Developers' Library)

Revision as of 17:00, 9 February 2013

Article Metadata
Code ExampleTested with
Devices(s): Nokia 6131, Nokia E70, Nokia N78, Nokia C3-01
CompatibilityArticle
Keywords: javax.microedition.lcdui.CustomItem
Created: vltsoy (27 Nov 2008)
Last edited: marcelobarrosalmeida (09 Feb 2013)

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: trashedDev (10 Aug 2012)
The full potential of this article isn't available on full-touch devices since this article needs to have a physical number pad.

Contents

Overview

This code snippet demonstrates how to create and display custom control. It consist of two files:

  1. CustomControl.java - Inherited from MIDlet, displays the MyItem control.
  2. MyItem.java - Inherited from CustomItem.

Custom contol class MyItem implements the method keyPressed(), which is responsible for storing the last key-pressed code. It also implements the paint() method, which displays the last key-pressed code inside the control.


Source file: CustomControl.java

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
 
 
public class CustomControl extends MIDlet implements CommandListener{
//Main form.
 
private Form form;
private Display display;
private Command exitCommand;
private MyItem myItem;
 
/**
* Constructor. Constructs the object and initializes displayables.
*/

public CustomControl() {
form = new Form("Custom Control MIDlet.");
 
// Add command buttons
exitCommand = new Command("Exit", Command.EXIT, 1);
form.addCommand(exitCommand);
form.setCommandListener(this);
 
display = Display.getDisplay(this);
display.setCurrent(form);
 
myItem = new MyItem("Custom control");
form.append(myItem);
}
 
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param cmd the command that was invoked
* @param displayable the displayable where the command was invoked
*/

public void commandAction(Command cmd, Displayable displayable) {
if (cmd == exitCommand) {
notifyDestroyed();
}
}
 
/**
* From MIDlet.
* Called when the MIDlet is started.
*/

public void startApp() {
// No implementation required.
}
 
/**
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.
*/

public void pauseApp() {
// No implementation required.
}
 
/**
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated
*/

public void destroyApp(boolean unconditional) {
// No implementation required.
}
}

Source file: MyItem.java

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
 
class MyItem extends CustomItem {
// Last key pressed code.
private int key;
 
/**
* Constructor.
*/

public MyItem( String label ){
super( "" );
}
 
/**
* From CustomItem.
* Called by the system to redraw canvas.
* @param graphics used for drawing operations.
*/

public void paint(Graphics g, int width, int height) {
//Display last key pressed.
String output = "Key code:"+String.valueOf(key);
 
g.drawString(output,(width-g.getFont().stringWidth(output))/2,
(height-g.getFont().getHeight())/2, 0);
 
g.setStrokeStyle(Graphics.DOTTED);
for (int i= 0;i<(height+width)/10;i++)
{
g.setColor(i*10000);
g.drawRect( 0+i, 0+i, (width-i*2), (height-i*2));
}
}
 
/**
* From CustomItem.
* Called by the system to retrieve minimum width required for this control.
*/

protected int getMinContentWidth() {
return 150;
}
 
/**
* From CustomItem.
* Called by the system to retrieve minimum height required for this control.
*/

protected int getMinContentHeight() {
return 100;
}
 
/**
* From CustomItem.
* Called by the system to retrieve preferred width for this control.
*/

protected int getPrefContentWidth(int arg0) {
return 200;
}
 
/**
* From CustomItem.
* Called by the system to retrieve preferred height for this control.
*/

protected int getPrefContentHeight(int arg0) {
return 100;
}
 
/**
* From CustomItem.
* Called by the system to redraw canvas.
* @param graphics used for drawing operations.
*/

protected void keyPressed(int keyCode) {
key = keyCode;
this.repaint();
}
}

Postconditions

After starting the MIDlet, the user will see custom control with the last key-pressed code.

Custom item.png

Supplementary material

You can view the source file and executable application in the attached zip archive. The archive is available for download at Media:Creating custom control in J2ME.zip

Related articles:

254 page views in the last 30 days.
×