Search code examples
java-me

JavaME read an input from a textfield and display it in another textfield


I have read a value from a textfield and I wanted to display it in another textfield, how do I do it, a piece of code will be help ful.

One more thing is I want to do the same thing as above but via a button.


Solution

  • import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.ItemCommandListener;
    import javax.microedition.lcdui.StringItem;
    import javax.microedition.lcdui.TextField;
    
    public class FormDemo extends Form implements ItemCommandListener{
        private TextField tf1;
        private TextField tf2;
        private StringItem button;
        private Command cmd_copy = new Command("Copy", Command.OK, 0);
        FormDemo(){
            super("Name");// Form Name
            tf1 = new TextField("Enter value", "", 32, TextField.ANY);
            tf2 = new TextField("Copy Here", "", 32, TextField.ANY);
            append(tf1);
            append(tf2);
            button = new StringItem("Submit", "Submit");
            append(button);
            button.addCommand(cmd_copy);
            button.setItemCommandListener(this);
    
        }
    
        public void commandAction(Command c, Item item) {
            tf2.insert(tf1.getString(), 0);
    
        }
    }
    

    how to use this

    display.setCurrent(new FormDemo());