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

    Question Please tell me what i am doing wrong in this code

    Hi,
    Hope you all will be fine. Actually it's a continuation of my previous post. Anyways what i did is this that this code create a form, two radio buttons and two commands. I also have two midlet in my project. One is for choosing options and otehr is what is shown after selecting the option. Logic is quite simple. I am stuck at ActionListener. Please tell me for radio buttons what action listener i write. Means what action listener my class implements. whether it is ActionListener or CommandListener. Right now my code implements ActionListener. Actually the approach is very simple. After writing actionPerformed(Actionevent e) method first i tried
    Java Code:
    Command selectedCommand = e.getcommand();
    if (selectedCommand.equals("Select"){
    }
    I also tried

    Java Code:
    if (selectedCommand.equals(select){      //select is the name of the command, Command select = new Command("Select");
    }
    but it didn't work i am unable to understand what is going wrong it says incompatible types but i couldn't understand . Then i want that there is another form in the second midlet which is actually a text box. Means if command = select then i want that form on second midlet is shown . I tried this in the actionPerformed

    Java Code:
    Command selectedCommand = e.getcommand();
    if (selectedCommadn.equals("Select){
        Translator translator = new Translator();
         f.show(translator)
    }
    Translator is my second midlet in the same project whom i want to show if user select "select" command. I know this is wrong but can you guide me how can i achieve this behaviour that when user select "select" command then the form on the second midlet is shown.
    Here is the code
    Java Code:
     
    public class ChooseOptions extends MIDlet implements ActionListener {
    
    
        public void startApp() {
    
            /**
             * You must initialize LWUIT before you can use it in your application.
             * To do this, call Display's static init() method, passing in the
             * application object. For example, in a MIDlet's startApp()  method,
             * initialize LWUIT like this:
             */
            Display.init(this);
    
            //This code creates a single empty form with a title.
            Form f = new Form("Hello, LWUIT");
    
            f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    
            RadioButton sms, internet;
            ButtonGroup group = new ButtonGroup();
    
            sms = new RadioButton("SMS");
            group.add(sms);
            f.addComponent(sms);
    
            internet = new RadioButton("Internet");
            group.add(internet);
            f.addComponent(internet);
    
            f.show();
    
            Command select = new Command("Select");
            Command exit = new Command("Exit");
            f.addCommand(select);
            f.addCommand(exit);
            f.addCommandListener(this);
         
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
        
        public void actionPerformed(ActionEvent e){
            Command selectedCommand = e.getCommand();
            if (selectedCommand.equals(select)){
                
                
            }
           
        }
    
        
    }
    I just tried this and i think this is fine but i again stuck how i move to another midlet when user choose select

    Java Code:
     public void actionPerformed(ActionEvent e){
            RadioButton preference = (RadioButton)e.getSource();
            String answer = preference.getText();
            if (answer.equalsIgnoreCase("Select")){
                   
            }
    Thank you.
    Last edited by Basit781; 05-19-2010 at 02:18 PM.

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

    Default

    You declared command inside the startApp method so you can't access it in another method (actionPerformed). Declare it outside the method as an instance variable of the class so you can do
    Java Code:
    if (selectedCommand == select)

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

    Default

    Hi,
    Thanks.But what about my last part. Is it correct

    Java Code:
     public void actionPerformed(ActionEvent e){
            RadioButton preference = (RadioButton)e.getSource();
            String answer = preference.getText();
            if (answer.equalsIgnoreCase("Select")){
                   
            }
    and how i move from one midlet to another. Like in JAVA SE we simply create constructror of the another class and then call method of it.

    Here is my second class which is in the same package. I want that the textbox that i created in this code is shown after choosing the option.

    Java Code:
    public class Translation extends MIDlet {
    
        private Form form;
        private TextField txtField;
        private TextBox txtBox1;
    
    
        public Translation(){
    
            //form = new Form("Welcome to VectraCom translation Page");
    
            //Similar as using a textBox
            //txtField = new TextField("Please enter text in English:", "", 50, TextField.ANY);
            //form.append(txtField);
    
            txtBox1 = new TextBox("Please enter text in English", "", 1000, TextField.ANY);
            //form.append(txtField);
        }
    
        public void startApp() {
    
            Display display = Display.getDisplay(this);
            display.setCurrent(txtBox1);
            //display.setCurrent(form);
    
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    }
    and i tried this but this isn't working
    Java Code:
     public void actionPerformed(ActionEvent e){
            RadioButton preference = (RadioButton)e.getSource();
            String answer = preference.getText();
            if (answer.equalsIgnoreCase("Select")){
                Translation translation = new Translation();
                  
            }
    
        }
    Any idea

    Thank you.

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

    Default

    After trying this i got this exception

    Java Code:
      public void actionPerformed(ActionEvent e){
            RadioButton preference = (RadioButton)e.getSource();
            String answer = preference.getText();
            if (answer.equalsIgnoreCase("Select")){
                Translation translation = new Translation();
                translation.startApp();
    
                  
            }
    
        }
    and the exception is

    Java Code:
    java.lang.ClassCastException:   0
            at ChooseOptions.actionPerformed(ChooseOptions.java:73)
            at com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
            at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:211)
            at com.sun.lwuit.Form.actionCommandImpl(Form.java:1220)
            at com.sun.lwuit.Form.actionCommandImpl(Form.java:1195)
            at com.sun.lwuit.Form$MenuBar.actionPerformed(Form.java:2676)
            at com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
            at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:211)
            at com.sun.lwuit.Button.fireActionEvent(Button.java:274)
            at com.sun.lwuit.Button.released(Button.java:295)
            at com.sun.lwuit.Form$MenuBar.keyReleased(Form.java:2948)
            at com.sun.lwuit.Form.keyReleased(Form.java:1731)
            at com.sun.lwuit.Display.handleEvent(Display.java:1232)
            at com.sun.lwuit.Display.edtLoopImpl(Display.java:636)
            at com.sun.lwuit.Display.mainEDTLoop(Display.java:591)
            at com.sun.lwuit.RunnableWrapper.run(RunnableWrapper.java:120)
            at java.lang.Thread.run(), bci=11
    line 73 is RadioButton preference = (RadioButton)e.getSource();

    Any idea

    Thanks

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

    Default

    Obviously the source of the ActionEvent is not a RadioButton. Generally you should not cast before doing an instanceof check to make sure your cast will succeed first.

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

    Default

    Hi,
    Thanks. This time i tried this and now the exception is

    Java Code:
      public void actionPerformed(ActionEvent e){
            Command selected = e.getCommand();
            String label = selected.getCommandName();
            if ((label.equals("Select")) && (sms.isSelected())){
                Translation translation = new Translation();
                translation.startApp();
    
            //RadioButton preference = (RadioButton)e.getSource();
            //String answer = preference.getText();
            //if (answer.equalsIgnoreCase("Select")){
                //Translation translation = new Translation();
               // translation.startApp();
    
                  
            }else if (label.equalsIgnoreCase("Exit")){
                notifyDestroyed();
            }
    
        }
    and the exception is

    Java Code:
    java.lang.SecurityException: MIDlet not constructed by createMIDlet.
            at com.sun.midp.midlet.MIDletStateHandler.newMIDletPeer(), bci=24
            at javax.microedition.midlet.MIDlet.<init>(), bci=9
            at Translation.<init>(Translation.java:16)
            at ChooseOptions.actionPerformed(ChooseOptions.java:76)
            at com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
            at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:211)
            at com.sun.lwuit.Form.actionCommandImpl(Form.java:1220)
            at com.sun.lwuit.Form.actionCommandImpl(Form.java:1195)
            at com.sun.lwuit.Form$MenuBar.actionPerformed(Form.java:2676)
            at com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
            at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:211)
            at com.sun.lwuit.Button.fireActionEvent(Button.java:274)
            at com.sun.lwuit.Button.released(Button.java:295)
            at com.sun.lwuit.Form$MenuBar.keyReleased(Form.java:2948)
            at com.sun.lwuit.Form.keyReleased(Form.java:1731)
            at com.sun.lwuit.Display.handleEvent(Display.java:1232)
            at com.sun.lwuit.Display.edtLoopImpl(Display.java:636)
            at com.sun.lwuit.Display.mainEDTLoop(Display.java:591)
            at com.sun.lwuit.RunnableWrapper.run(RunnableWrapper.java:120)
            at java.lang.Thread.run(), bci=11
    Any idea what this mean MIDlet not constructed by createMIDlet.

    Thanks

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

    Default

    You should not be atempting to create new Midlets. Rather read up on how to use layouts so you can dynamically update your Midlet panel with new controls if you need to.

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

    Default

    Hi,
    Thanks. Ok Fine. Just tell me, the thing that i am trying to do is possible. Means when i select the "select" button it shows me textbox that is described in the other medlet.

    First i thought that i put the whole code in one midlet only. But then i think it's easy for me to write two midlets one is for choosing option and the second is for the result of choosing.

    Thanks.

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

    Default

    As I said, don't create multiple midlets. Instead create multiple forms.
    Then in your midlet you declare a display object

    Java Code:
    protected Display display;
    which you initialize in your startApp method as follows
    Java Code:
    display = Display.getDisplay(this);
    Then you create your first form and set it as your midlet's display using


    Java Code:
    display.setCurrent(form);
    When you want to change the components being displayed simply set the new Form with the new components as your midlet's current display using the setCurrent method.

    P.S You might need to control when the startApp method is allowed to reset it's current display by using some booleans or similar.

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

    Default

    Hi,
    Thanks.
    what do you mean by this,
    don't create multiple midlets. Instead create multiple forms.
    you want to say that i should write only one midelte and in this midelet i made a text box, as my midelet already contain radiobuttons and bla bla bla and then display it when the button select. Is it.

    Thanks

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

    Default

    Do read my reply again. Add your controls to a form and set the form as the midlet's display. Then when you want to change what is displayed on the midlet you create a form with the new content and set that form as the midlets's display.

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

    Default

    Hi,
    Thanks for your help. But now i am stuck at a point. Actually in form2 I created two textfields and i want that when form2 shown then the focus is on txtarea1 means a cursor is blinking in textarea1 and user type any thing in it they want. But what is happening right now is this, form2 shown but i am unable to type anything in the textArea. Although the methods are working means txtArea1.setText() is working but i want that user is also able to type in the txtArea1. Please guide me how can i do this or what i am doing logically incorrect.
    Actually i thought that since i add txtarea1 to the container and then add container to the form. So the flow is form.setfocused(container.getcomponent) but it's isn't working. so please guide me.
    Here is the code

    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.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.setFocus(true);
            txtArea1.setGrowByContent(true);
            txtArea1.setText("hi how r u asdsadsadasdasdasdasdasdasdasdasdsadsadsad");
            txtArea1.setEnabled(true);
            
            //adding txtArea1 to container
            textBoxContainer.addComponent(txtArea1 );
            
              //create text Area2
            txtArea2 = new TextArea(2, 5);
            //txtArea1.setFocus(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.setFocused(textBoxContainer.getComponentAt(0));
    	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) {
        }
    }
    Thank you.

Similar Threads

  1. What is wrong with my code?!
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 09:01 PM
  2. Need help! something wrong in my code
    By novak100 in forum New To Java
    Replies: 2
    Last Post: 11-19-2009, 12:59 AM
  3. pls tell wat wrong with my code???
    By low224 in forum New To Java
    Replies: 13
    Last Post: 01-11-2009, 08:40 AM
  4. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 12:26 PM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-23-2008, 12:55 AM

Posting Permissions

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