Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: make textbox in canvas

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default make textbox in canvas

    Hi , i want to make textbox in canvas to give some data from user. i use this code , but i have probleam in change between to textbox , whene i press DOWN or UP my text move too !!!,please help me to modify this code
    tanks
    import javax.microedition.lcdui.*;
     
    public class MIDPCanvas extends Canvas {
     
        String keyName,bufferText,text2,text="";
        int currentX = 15;
        int currentY = 45;
     
     
        MIDPCanvas(){
            setFullScreenMode(true);
        }
     
        protected void paint(Graphics g) {
     
     
            g.setColor(211,211,211);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(0,0,0);
            g.drawString("  Price", 113, 15, Graphics.TOP | Graphics.HCENTER);
            g.setColor(255,255,255);
            g.fillRect(10, 30, 218, 40);
            g.setColor(0);
            g.drawString("Magsad", 100, 80, 0);
            g.setColor(255,255,255);
            g.fillRect(10, 100, 218, 40);
            g.setColor(0,0,0);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
     
            g.drawString(text, currentX, currentY,0);
            g.drawString(text2,15,110,0);
     
        }
        protected void keyPressed(int keyCode){
     
           keyName = getKeyName(keyCode);     
           if (keyName.equals("DOWN")){
                  currentX=15;
                  currentY=110;
     
     
           }
           else if(keyName.equals("UP")){
                 currentX = 15;
                 currentY = 45;
           }
           else{
               key();
           }
           repaint();
           }
     
     
      public String key(){
     
           if(keyName.equals("0")){
            bufferText = "0";
            text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("1")){
               bufferText = "1";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("2")){
               bufferText = "2";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("3")){
               bufferText = "3";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("4")){
               bufferText = "4";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("5")){
               bufferText = "5";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("6")){
               bufferText = "6";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("7")){
               bufferText = "7";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("8")){
               bufferText = "8";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
           else if(keyName.equals("9")){
               bufferText = "9";
               text = (text.length()>=23)?( text) :( text + bufferText);
           }
         //  else if (keyName.equals("DOWN")){
          //     currentX=15;
         //      currentY=110;
        //   }
         //  else if(keyName.equals("UP")){
         //        currentX = 15;
         //        currentY = 45;
         //  }
           else{
               text = keyName;
           }
               return text;
           }
     
        }


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: make textbox in canvas

    Here you go:

    int currentX1 = 15;
    int currentY1 = 45;
    int currentX2 = 15;
    int currentY2 = 110;
     
    protected void paint(Graphics g) {
     
            g.setColor(211,211,211);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(0,0,0);
            g.drawString("  Price", 113, 15, Graphics.TOP | Graphics.HCENTER);
            g.setColor(255,255,255);
            g.fillRect(10, 30, 218, 40);
            g.setColor(0);
            g.drawString("Magsad", 100, 80, 0);
            g.setColor(255,255,255);
            g.fillRect(10, 100, 218, 40);
            g.setColor(0,0,0);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
     
            g.drawString(s_text1,currentX1, currentY1,0);
            g.drawString(s_text2,currentX2, currentY2,0);
     
    }
            static boolean b_upText=true;
    	static String s_text1="";
    	static String s_text2="";
    protected void keyPressed(int keyCode){
    		String keyName = getKeyName(keyCode); 
    		if (keyName.equals("DOWN") || keyName.equals("UP")){
    			b_upText=!b_upText;
    		}
    		else
    		{
    			if(b_upText)
    			{
    				s_text1+=keyName;
    			}
    			else 
    			{
    				s_text2+=keyName;
    			}
                            repaint();
    		}
    }

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: make textbox in canvas

    tanks my frieand

Similar Threads

  1. How to create directory in Java?
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 24th, 2011, 12:53 PM
  2. How can i make the components resizable?
    By ces_31 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 1st, 2009, 10:46 AM
  3. [SOLVED] How to make a integer negative if it meets a certain criteria?
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 14th, 2009, 02:27 PM
  4. Best beginners project in Java
    By Fendaril in forum Java Theory & Questions
    Replies: 3
    Last Post: February 10th, 2009, 08:52 AM
  5. Java algorithm for bank program to connect database
    By araujo3rd in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 10th, 2008, 01:34 PM