Results 1 to 6 of 6
  1. #1
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Question How to get focus on components in LWUIT form

    Hi,
    Hope you all will be fine. I created a form with two text areas in it. Now i want when my form is shown, the focus is set on txtArea1, means as form show there is a cursor blinking in the txtArea1 so user can type any thing in it. How can i do it.
    here is the code

    Java Code:
    	//Setup Form 2
    	form2 = new Form("Form 2");
    
            //set Form layout
    	form2.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    
            //Create container that holds component and set its layout
    //        Container textBoxContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    
            //create text Area
            txtArea1 = new TextArea(2, 5);
            txtArea1.setFocusable(true);
            txtArea1.setFocus(true);
            txtArea1.setGrowByContent(true);
           // txtArea1.setText("hi how r u asdsadsadasdasdasdasdasdasdasdasdsadsadsad");
            //txtArea1.setFocusable(true);
    
            //adding txtArea1 to container
            //textBoxContainer.addComponent(txtArea1 );
            
              //create text Area2
            txtArea2 = new TextArea(2, 5);
            //adding txtArea1 to container
            //textBoxContainer.addComponent(txtArea2 );
    
    	//form2.addComponent(0,null,new Label("This is the second Form"));
            //form2.addComponent(textBoxContainer);
    	form2.addCommand(cmdSend);
    	form2.addCommand(cmdBack);
    	form2.addCommandListener(this);
            form2.addComponent(txtArea1);
            form2.addComponent(txtArea2);
            form2.setFocusable(true);
            form2.setFocused(txtArea1);
    Thank you.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Is there a requestFocus method in your textarea class?

  3. #3
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default

    Hi,
    Thanks. No. Here is the complete code. Some things i tried but wasn't working that's why i commented it out.

    Java Code:
    public class ChooseOptions2 extends MIDlet implements ActionListener {
    
        Form form1;
        Form form2;
    
        RadioButton sms, internet;
    
        ButtonGroup group;
    
        TextArea txtArea1;
        TextArea txtArea2;
    
        Command cmdSelect = new Command("Select");
        Command cmdBack = new Command("Back");
        Command cmdExit = new Command("Exit");
        Command cmdSend = new Command("Send");
    
        public ChooseOptions2(){
    
            //Initialize the LWUIT
            Display.init(this);
        }
    
        protected void startApp() throws MIDletStateChangeException {
            //Load the Theme
    //        Resources r;
    //	try {
    //	    r = Resources.open("/LWUITtheme.res");
    //			UIManager.getInstance().
    //			setThemeProps(r.getTheme("LWUITDefault"));
    //	    } catch (IOException e) {
    //			e.printStackTrace();
    //              }
    
            group = new ButtonGroup();
    
            sms = new RadioButton("SMS");
            group.add(sms);
    
    
            internet = new RadioButton("Internet");
            group.add(internet);
    
    
    	//Setup Form 1
    	form1 = new Form("Please Select Options");
    	form1.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            form1.addComponent(sms);
            form1.addComponent(internet);
    	//form1.addComponent(BorderLayout.NORTH, new Label("My First Form"));
    	//form1.addComponent(BorderLayout.WEST, new Label("WEST"));
    	//form1.addComponent(BorderLayout.CENTER, new Label("CENTER"));
    	//form1.addComponent(BorderLayout.EAST, new Label("EAST"));
    	//form1.addComponent(BorderLayout.SOUTH, new Label("Click Rotate"));
    	form1.addCommand(cmdSelect);
    	form1.addCommand(cmdExit);
    	form1.addCommandListener(this);
    	form1.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 1000));
    
    	//Setup Form 2
    	form2 = new Form("Form 2");
    
            //set Form layout
    	form2.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    
            //Create container that holds component and set its layout
    //        Container textBoxContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    
            //create text Area
            txtArea1 = new TextArea(2, 5);
            txtArea1.setFocusable(true);
            txtArea1.setFocus(true);
            txtArea1.setGrowByContent(true);
           // txtArea1.setText("hi how r u asdsadsadasdasdasdasdasdasdasdasdsadsadsad");
            //txtArea1.setFocusable(true);
    
            //adding txtArea1 to container
            //textBoxContainer.addComponent(txtArea1 );
            
              //create text Area2
            txtArea2 = new TextArea(2, 5);
            //txtArea1.setFocus(true);
                    txtArea1.setEnabled(true);
            txtArea1.setFocusable(true);
            txtArea1.setFocusPainted(true);
            //adding txtArea1 to container
            //textBoxContainer.addComponent(txtArea2 );
    
    	//form2.addComponent(0,null,new Label("This is the second Form"));
            //form2.addComponent(textBoxContainer);
    	form2.addCommand(cmdSend);
    	form2.addCommand(cmdBack);
    	form2.addCommandListener(this);
            form2.addComponent(txtArea1);
            form2.addComponent(txtArea2);
            form2.setFocusable(true);
            form2.setFocused(txtArea1);
    
           // form2.setFocused(textBoxContainer.getComponentAt(0));
           // System.out.println("the focus is on: " + form2.getFocused() );
    	form2.setTransitionInAnimator(Transition3D.createCube(1000, true));
    	form1.show();
        }
    
        public void actionPerformed(ActionEvent evt) {
    		//check which command cliked
    		if (evt.getCommand()==cmdExit) {
    			notifyDestroyed();
    		} else if (evt.getCommand()==cmdSelect) {
    			form2.show();
    		} else if (evt.getCommand()==cmdBack) {
    			form1.show();
    		}
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    }
    Thanks

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The method is there (available through inheritance). See the javadocs here.
    Use the requestFocus method to request focus on components.
    You should not be writing any code without the javadocs.

  5. #5
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default

    when i add request focus like this

    Java Code:
            txtArea1 = new TextArea(2, 5);
            txtArea1.requestFocus();
            txtArea1.setGrowByContent(true);
           // txtArea1.setText("hi how r u asdsadsadasdasdasdasdasdasdasdasdsadsadsad");
            txtArea1.setFocusable(true);
    I got errors. Tell me one thing on which component i request focus to. Means . I added two textAreas to the form. So should it be form.requestfocus() or txtArea.requestfocus.

    Here are the errors

    Compiling 1 source file to C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\buil d\compiled
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:7: cannot find symbol
    symbol : class Component
    location: class FocusAnimation
    private Component futureFocus;
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:8: cannot find symbol
    symbol : class Motion
    location: class FocusAnimation
    private Motion focusMotionX;
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:9: cannot find symbol
    symbol : class Motion
    location: class FocusAnimation
    private Motion focusMotionY;
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:10: cannot find symbol
    symbol : class Motion
    location: class FocusAnimation
    private Motion focusMotionWidth;
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:11: cannot find symbol
    symbol : class Motion
    location: class FocusAnimation
    private Motion focusMotionHeight;
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:22: cannot find symbol
    symbol : class Component
    location: class FocusAnimation
    public void setFocused(Component c) {
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:15: cannot find symbol
    symbol : class Button
    location: class FocusAnimation
    addComponent(new Button("Button " + iter));
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:17: cannot find symbol
    symbol : class TextArea
    location: class FocusAnimation
    addComponent(new TextArea("Multi-line text\nwith several\ndifferent lines", 3, 20));
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:18: cannot find symbol
    symbol : constructor List(java.lang.Object[])
    location: class javax.microedition.lcdui.List
    addComponent(new List(new Object[] {"Entry 1", "Entry 2", "Entry 3"}));
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:19: cannot find symbol
    symbol : class ComboBox
    location: class FocusAnimation
    addComponent(new ComboBox(new Object[] {"Entry 1", "Entry 2", "Entry 3"}));
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:23: cannot find symbol
    symbol : class Component
    location: class FocusAnimation
    Component current = getFocused();
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:23: cannot find symbol
    symbol : method getFocused()
    location: class FocusAnimation
    Component current = getFocused();
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:26: cannot find symbol
    symbol : variable Motion
    location: class FocusAnimation
    focusMotionX = Motion.createSplineMotion(current.getAbsoluteX(), futureFocus.getAbsoluteX(), 300);
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:27: cannot find symbol
    symbol : variable Motion
    location: class FocusAnimation
    focusMotionY = Motion.createSplineMotion(current.getAbsoluteY(), futureFocus.getAbsoluteY(), 300);
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:28: cannot find symbol
    symbol : variable Motion
    location: class FocusAnimation
    focusMotionWidth = Motion.createSplineMotion(current.getWidth(), futureFocus.getWidth(), 300);
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:29: cannot find symbol
    symbol : variable Motion
    location: class FocusAnimation
    focusMotionHeight = Motion.createSplineMotion(current.getHeight(), futureFocus.getHeight(), 300);
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:34: cannot find symbol
    symbol : method setFocused(<nulltype>)
    location: class javax.microedition.lcdui.Form
    super.setFocused(null);
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:41: cannot find symbol
    symbol : method paint(javax.microedition.lcdui.Graphics)
    location: class javax.microedition.lcdui.Form
    super.paint(g);
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\src\ FocusAnimation.java:51: cannot find symbol
    symbol : method animate()
    location: class javax.microedition.lcdui.Form
    boolean val = super.animate();
    19 errors
    C:\Documents and Settings\vc\My Documents\NetBeansProjects\MobileApplication1\nbpr oject\build-impl.xml:368: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 5 seconds)
    what's this mean
    Thanks

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You call requestFocus on the TextArea that you want to get the focus.
    Read the error messages you are getting. They tell you hints about the problem and reading them allows you to attempt to fix the problem yourself which is a good thing.
    They are coming from a class called FocusAnimation. Is it missing some import statements?

Similar Threads

  1. LWUIT Table
    By Gustavo in forum CLDC and MIDP
    Replies: 0
    Last Post: 05-11-2010, 02:19 PM
  2. Replies: 1
    Last Post: 03-22-2010, 09:03 AM
  3. Replies: 0
    Last Post: 10-31-2009, 09:24 AM
  4. Replies: 10
    Last Post: 08-12-2009, 04:02 PM
  5. closing form and giving focus to all parents?
    By saeed144 in forum New To Java
    Replies: 3
    Last Post: 04-08-2009, 04:11 PM

Posting Permissions

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