×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    148

    Input user data into the textfield.

    I have drawn a textfield (i have not used FORM or TEXTFIELD option) everything is done by draw and fill rectangle.
    Now i need to input, user data into the textfield.

    If i had used the form, my problem would have been solved but according to the requirement everything should be drawn on the canvas.
    Can anyone help me in this?

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: Input user data into the textfield.

    Hi,
    I hope you can do it with this code segment..


    import javax.microedition.lcdui.*;
    import java.util.*;


    public class Itching_C extends Canvas implements CommandListener
    {

    textBox tb;

    /********Screen Related Data*********/
    final int wd=getWidth ();
    final int ht=getHeight ();
    private int cursor=0,offset=0,flag=0;
    //flag=0 means 0<cursor<138
    //flag=1 means cursor=0
    //flag=2 means cursor=138
    Image bg,title,txtBox,entque;
    tTask tsk=null;

    //private StringBuffer text= new StringBuffer("When I will Get Job.");

    //private StringBuffer text= new StringBuffer("abcdefghijklmonpqrstuvwxyz");
    private StringBuffer text= new StringBuffer ("");
    //private StringBuffer text= new StringBuffer("A");
    private int time=22;
    private int state=0;//state=0=>First Character In Caps other are In small
    //state=1 => In Capital.
    //state=2 => In Small.
    private int keyPrs=0,blink=0,start=0,end=0,index=0,count=0;
    private int cursorInd=0;
    private boolean Caps=true,add=true;
    private int code=65;




    /********End Of Screen Related Data*********/

    /*******Commands Of The Screen*********/
    Command cSub = new Command ("Show Data",1,1);
    Command cBck = new Command ("Back",1,1);
    /*********End Of Commands*********/

    Itching_C (textBox tb)
    {
    this.tb = tb;
    try
    {
    txtBox=Image.createImage ("/tz2.png");
    entque=Image.createImage ("/entque.png");
    bg=Image.createImage ("/bg.png");
    title=Image.createImage ("/title.png");
    }
    catch(Exception e)
    {
    System.out.println ("Error"+e);
    }

    starttm ();
    setCmd ();
    }
    /***********Begining Of Command Related Functions*************/
    public void setCmd ()
    {
    remCmd ();
    this.addCommand (cSub);
    this.addCommand (cBck);
    this.setCommandListener (this);
    repaint ();
    }

    public void remCmd ()
    {
    removeCommand (cSub);
    removeCommand (cBck);
    }

    public void commandAction (Command c, Displayable d)
    {
    if(c==cBck)
    {
    tb.destroyApp (true);
    tb.notifyDestroyed ();
    }
    else if(c==cSub)
    {
    System.out.println (text);
    }
    }

    /***********End Of Command Related Functions*************/
    public void paint (Graphics g)
    {
    screenDisp (g);
    status ();
    // if(time==20)
    // addText ();
    }

    public void pointerPressed (int x,int y)
    {

    }


    public void keyPressed (int keyCode)
    {
    switch(keyCode)
    {
    case -3://KEY_LEFT_ARROW
    left ();
    break;
    case -4://KEY_RIGHT_ARROW
    right ();
    break;
    case -8://CLEAR_KEY
    if(cursorInd>0)
    {
    text.deleteCharAt (cursorInd-1);
    cursorInd--;
    if(cursorInd==0)
    state=0;
    }
    break;
    case 35://#_KEY
    doChange (35);
    break;
    case 48://KEY_NUM_0
    doChange (0);
    break;
    case 49://KEY_NUM_1
    doChange (1);
    break;
    case 50://KEY_NUM_2
    doChange (2);
    break;
    case 51://KEY_NUM_3
    doChange (3);
    break;
    case 52://KEY_NUM_4
    doChange (4);
    break;
    case 53://KEY_NUM_5
    doChange (5);
    break;
    case 54://KEY_NUM_6
    doChange (6);
    break;
    case 55://KEY_NUM_7
    doChange (7);
    break;
    case 56://KEY_NUM_8
    doChange (8);
    break;
    case 57://KEY_NUM_9
    doChange (9);
    break;
    }
    }

    private void doChange (int key)
    {
    // if (keyPrs != key && keyPrs != 0 && time != 22)
    // {
    // addText ();
    // }
    // else
    // {
    time = 0;
    text.insert (cursorInd, (char) (48 + key));
    cursorInd++;
    time = 22;
    // }
    if(state==1)
    state=2;
    else if(state==2)
    state=3;
    else
    state=1;

    }

    public void keyReleased (int keyCode)
    {

    }

    private void left ()
    {
    if(cursorInd>0)
    cursorInd--;
    if(flag==1 && offset>0)
    count++;
    }

    private void right ()
    {
    if(text.length ()>cursorInd)
    cursorInd++;
    if(count>0)
    count--;
    }


    private void change (int start,int end)
    {

    if(index<end)
    index++;
    else
    index=start;
    }

    boolean check_number=false; //

    private void status ()
    {
    code = 49;
    }


    private void addText ()
    {
    if(text.length ()<5)
    {
    text.insert (cursorInd,(char)(code+index));
    time=22;
    cursorInd++;
    }
    }

    private void screenDisp (Graphics g)
    {

    Font f=Font.getDefaultFont ();

    if(offset<0)
    offset=0;


    if(f.stringWidth (""+text)>=138)
    {
    offset=f.stringWidth ((""+text).substring (0,text.length ()-count))-138;
    if(offset<=0)
    offset=0;
    cursor=138-((f.stringWidth (""+text))-(f.stringWidth ((""+text).substring (0,cursorInd))));
    if(cursor<=0)
    {
    cursor=0;
    flag=1;
    }
    else if(cursor>=138)
    {
    cursor=138;
    flag=2;
    }
    else
    flag=0;
    }
    else
    {
    offset=0;
    cursor=(f.stringWidth ((""+text).substring (0,cursorInd)));
    if(cursor>=138)
    {
    cursor=138;
    flag=0;
    }
    }

    int top=ht/2-bg.getHeight ()/2+1;
    int start=wd/2-txtBox.getWidth ()/2+10;

    g.setColor (74,164,255);
    g.fillRect (0,0,wd,ht);

    g.drawImage (bg,wd/2,ht/2,g.HCENTER | g.VCENTER);
    g.drawImage (title,wd/2,top+7,Graphics.HCENTER | Graphics.VCENTER);
    top+=title.getHeight ()+19;

    g.setColor (0);

    g.drawImage (entque,wd/2,top,g.TOP|g.HCENTER);
    top+=33;
    g.drawImage (txtBox,wd/2,top-10,g.TOP|g.HCENTER);
    String strStar = "";
    g.setClip (wd/2-txtBox.getWidth ()/2+10,top-2,txtBox.getWidth ()-27,txtBox.getHeight ());
    if(time<20)
    {
    g.drawString ((""+text).substring (0,cursorInd)+(char)(code+index)+(""+text).substring (cursorInd,text.length ()),start-offset,top-2,g.LEFT|g.TOP);
    }
    else
    {
    g.drawString (""+text,start-offset,top-2,g.LEFT|g.TOP);
    g.setClip (0,0,wd,ht);
    if(blink<10)
    {
    System.out.println("start+cursor -- "+start+cursor);
    g.drawString ("|",start+cursor,top-2,g.LEFT|g.TOP);
    }
    }
    }
    //Starting The Timer Of This Class.
    public void starttm ()
    {
    tsk=new tTask ();
    }

    public void stoptm ()
    {
    if(tsk!=null)
    {
    tsk.tm.cancel ();
    tsk.tm=null;
    tsk=null;
    }
    }

    /***************Timer Task Inner Class*****************/
    public class tTask extends TimerTask
    {
    Timer tm=null;
    public tTask ()
    {
    tm=new Timer ();
    tm.schedule (this,0,30);
    }

    public void run ()
    {
    repaint ();
    if(time<20)
    time++;
    if(blink<20)
    blink++;
    else
    blink=0;
    }
    }
    }
    Last edited by raj_J2ME; 2009-04-23 at 08:24.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  3. #3
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    148

    Re: Input user data into the textfield.

    Thanks Raj.

Similar Threads

  1. How to make a user input field in Canvas?
    By pillar in forum Mobile Java General
    Replies: 7
    Last Post: 2006-07-07, 21:52
  2. Half Duplex Audio Streaming
    By subhrajyotisaha in forum Symbian Media (Closed)
    Replies: 0
    Last Post: 2006-05-25, 04:39
  3. Howto parse user input? */# - is it USSD or other command?
    By keypad in forum PC Suite API and PC Connectivity SDK
    Replies: 2
    Last Post: 2006-02-13, 14:46
  4. User Data Header in Binary
    By sanjnokia in forum General Messaging
    Replies: 4
    Last Post: 2004-06-05, 19:53
  5. MMS and User Input
    By kevinhankinson in forum General Messaging
    Replies: 3
    Last Post: 2004-03-15, 13:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×