Results 1 to 12 of 12
- 05-19-2010, 01:00 PM #1
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
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
I also triedJava Code:Command selectedCommand = e.getcommand(); if (selectedCommand.equals("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 actionPerformedJava Code:if (selectedCommand.equals(select){ //select is the name of the command, Command select = new Command("Select"); }
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.Java Code:Command selectedCommand = e.getcommand(); if (selectedCommadn.equals("Select){ Translator translator = new Translator(); f.show(translator) }
Here is the code
I just tried this and i think this is fine but i again stuck how i move to another midlet when user choose selectJava 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)){ } } }
Thank you.Java Code:public void actionPerformed(ActionEvent e){ RadioButton preference = (RadioButton)e.getSource(); String answer = preference.getText(); if (answer.equalsIgnoreCase("Select")){ }Last edited by Basit781; 05-19-2010 at 01:18 PM.
- 05-19-2010, 01:45 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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)
- 05-19-2010, 02:01 PM #3
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
Hi,
Thanks.But what about my last part. Is it correct
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.Java Code:public void actionPerformed(ActionEvent e){ RadioButton preference = (RadioButton)e.getSource(); String answer = preference.getText(); if (answer.equalsIgnoreCase("Select")){ }
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.
and i tried this but this isn't workingJava 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) { } }
Any ideaJava Code:public void actionPerformed(ActionEvent e){ RadioButton preference = (RadioButton)e.getSource(); String answer = preference.getText(); if (answer.equalsIgnoreCase("Select")){ Translation translation = new Translation(); } }
Thank you.
- 05-19-2010, 02:52 PM #4
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
After trying this i got this exception
and the exception isJava 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(); } }
line 73 is RadioButton preference = (RadioButton)e.getSource();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
Any idea
Thanks
- 05-19-2010, 02:58 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 05-19-2010, 03:14 PM #6
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
Hi,
Thanks. This time i tried this and now the exception is
and the exception isJava 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(); } }
Any idea what this mean MIDlet not constructed by createMIDlet.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
Thanks
- 05-19-2010, 03:30 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 05-19-2010, 03:40 PM #8
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
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.
- 05-19-2010, 03:53 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
As I said, don't create multiple midlets. Instead create multiple forms.
Then in your midlet you declare a display object
which you initialize in your startApp method as followsJava Code:protected Display display;
Then you create your first form and set it as your midlet's display usingJava Code:display = Display.getDisplay(this);
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.Java Code:display.setCurrent(form);
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.
- 05-19-2010, 04:19 PM #10
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
Hi,
Thanks.
what do you mean by this,
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.don't create multiple midlets. Instead create multiple forms.
Thanks
- 05-19-2010, 04:28 PM #11
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 05-20-2010, 05:08 PM #12
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
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
Thank you.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) { } }
Similar Threads
-
What is wrong with my code?!
By new2java2009 in forum New To JavaReplies: 2Last Post: 04-17-2010, 08:01 PM -
Need help! something wrong in my code
By novak100 in forum New To JavaReplies: 2Last Post: 11-18-2009, 11:59 PM -
pls tell wat wrong with my code???
By low224 in forum New To JavaReplies: 13Last Post: 01-11-2009, 07:40 AM -
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks