0
public class StateMachine extends StateMachineBase implements ActionListener {
  Resources resources;
 RadioButton Verifi=new RadioButton("Verification") ;
   RadioButton Enroll=new RadioButton("Enrollment");
StateMachineBase cl=new StateMachineBase()
        {};
ButtonGroup bg=new ButtonGroup();
static Form fo,f;
public StateMachine(String resFile) {
        super(resFile);
    }
  StateMachine()
    {

try{
    resources = Resources.open("/NEW AADHAR.res");
}
catch(java.io.IOException err)
{ err.printStackTrace(); }
cl.setHomeForm("Welcome");
 //fo = (Form)cl.startApp(resources,null,true);
fo=Display.getInstance().getCurrent();
f=cl.findWelcome(fo);
Verifi=cl.findVerification(f);
Enroll=cl.findEnrollment(f);
bg.add(Enroll);
bg.add(Verifi);
//f.addCommandListener(this);
Verifi.addActionListener(listener);Enroll.addActionListener(listener);
}
  protected  void initVars() {
    }

public void actionPerformed(ActionEvent ae) {

    throw new UnsupportedOperationException("Not supported yet.");
}       

ActionListener listener=new ActionListener(){
     protected void onWelcome_ButtonAction(Component c, ActionEvent event)
        {
Verifi.addActionListener(listener);
if(Verifi.hasFocus())
    {
    showForm("Login",null);

    }
    else if (Enroll.hasFocus())
    {
    showForm("Authentication",null);

   }
    else
        Dialog.show("INFORMATION","Select","OK","Cancel");

     }

        public void actionPerformed(ActionEvent ae) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

};
}
bharath
  • 14,283
  • 16
  • 57
  • 95
gaurav
  • 3
  • 2
  • ok fine,but i am confuse about my above question – gaurav Jul 14 '11 at 12:20
  • In your StateMachine class where you initializing the `Enrollment, Verification and bg`? – bharath Jul 18 '11 at 10:45
  • public class StateMachine extends StateMachineBase implements ActionListener { Resources resources; StateMachineBase cl=new StateMachineBase() {}; RadioButton bgVerification; RadioButton bgEnrollment; ButtonGroup bg=new ButtonGroup();; – gaurav Jul 19 '11 at 10:58
  • here in statemachine class and above constructor – gaurav Jul 19 '11 at 10:58
  • But see object not initialized. thats why you getting null exception. – bharath Jul 19 '11 at 11:04
  • which object???????????? – gaurav Jul 19 '11 at 11:18
  • fo = (Form)cl.startApp(resources,"/NEW AADHAR.res",true); f=cl.findWelcome(fo); f.addCommandListener(this); bgVerification=cl.findVerification(fo); bgEnrollment=cl.findEnrollment(fo); bg.add(bgEnrollment); bg.add(bgVerification); this is fine or should i make changes in above code to get form(welcome) radio button reference – gaurav Jul 19 '11 at 11:36
  • See Update 3 in my answer and use that code. – bharath Jul 19 '11 at 13:16

1 Answers1

0

Give the group name for RadioButton on ResourceEdit#GUI(use the same name for each RadioButton). Use following method is return the RadioButton. So use this method.

public com.sun.lwuit.RadioButton findRadioButton(Container root) {
   return (com.sun.lwuit.RadioButton)findByName("RadioButton", root);
        //root - Pass the RadioButton added Component.
}

Update 1:

Use this code for getting the form from generated class,

GeneratedClass genClass = new GeneratedClass() { };
final Form form = (Form) genClass.startApp(resources, null, true);

GeneratedClass - Use your class. resources - Use your resource edit

pass that form to findRadioButton(...);


Update 2:

you given 2 RadioButton called Verification and Enrollment. So use the following code,

 GeneratedClass genClass = new GeneratedClass() { };
 Form form = (Form) genClass.startApp(resources, null, true);
 RadioButton rbVerification = genClass.findVerification(form);
 RadioButton rbEnrollment = genClass.findEnrollment(form);

And check the RadioButton Group name on ResourceEdit#GUI. And give the same name for both RadioButton.


Update 3:

Call StateMachine() in your mainMidlet.java class.

public class StateMachine extends StateMachineBase implements ActionListener {

    Resources resources;
    RadioButton Verification;
    RadioButton Enrollment;

    public StateMachine(String resFile) {
        super(resFile);
    }

    /**
     * this method should be used to initialize variables instead of
     * the constructor/class scope to avoid race conditions
     */
    StateMachine() {
    }

    protected void initVars() {
    }

    /**
     * @param c
     * @param event
     */
    public void actionPerformed(ActionEvent ae) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    protected boolean onWelcomeEXIT() {
        boolean val = super.onWelcomeEXIT();
        return val;
    }

    protected void onWelcome_ButtonAction(Component c, ActionEvent event) {

        super.onSampleRB_RadioButtonAction(c, event);
        Verification = (RadioButton) c;  // initialize here
        if (Verification.hasFocus()) {
           showForm("Login",null);
        } else {
           Dialog.show("INFORMATION", "Please select option", "OK", "Cancel");
        }
    }
}
bharath
  • 14,283
  • 16
  • 57
  • 95
  • Thanks,Bharath.I get your point plz tell me that i have to call findRadioButton() method or findByName() method and in root i have to pass form name on which I have added radio buttons? – gaurav Jul 15 '11 at 05:09
  • If you want to get the `RadioButton` means use `findRadioButton()` and where you adding the `RadioButton`? – bharath Jul 15 '11 at 05:11
  • i have add my radion button on form – gaurav Jul 15 '11 at 05:29
  • hey Bharath what do you mean by resources in – gaurav Jul 15 '11 at 06:07
  • in method final Form form = (Form) genClass.startApp(resources, null, true) – gaurav Jul 15 '11 at 06:08
  • Pass your `Resources` instance. For ex, `Resources resources = Resources.open("/sample.res");` – bharath Jul 15 '11 at 06:28
  • i have done this in statemachine class but it gives error can not find varaible Resources – gaurav Jul 15 '11 at 07:07
  • plz can you give me small example for radio button works in statemachine class – gaurav Jul 15 '11 at 10:06
  • when i run lwuit desktop project,it gives protected boolean onWelcomeOK() { if(r2.isSelected())///error null pointer exception showForm("Login",null); boolean val = super.onWelcomeOK(); return val;} where r2 is radio button and when i press OK button it give error – gaurav Jul 15 '11 at 10:11
  • actually i don't understand about multiple methods(for e.g for lets say button) declare in statemachinebase class and how to use them – gaurav Jul 15 '11 at 10:17
  • protected void initVars() { try{ resources = Resources.open("/NEW AADHAR.res"); } catch(java.io.IOException err) { err.printStackTrace(); } Form f=(Form) cl.startApp(resources,null, true); f.addCommandListener(this); bgVerification=cl.findVerification(f); bgEnrollment=cl.findEnrollment(f); } – gaurav Jul 15 '11 at 11:34
  • i put this (init()) method code in statemachine class but i got null pointer exception at Form f=(Form) cl.startApp(resources,null, true); when i run it. protected boolean onWelcomeOK() { // If the resource file changes the names of components this call will break notifying you that you should fix the code if(bgVerification.isSelected()) showForm("Login",null); boolean val = super.onWelcomeOK(); return val; } this is my OK button code – gaurav Jul 15 '11 at 11:36
  • Better i feel create the new project in your netbeans and generate the class file from ResourceEdit. – bharath Jul 15 '11 at 11:45
  • Hi bharath,can u sort me out i have welcome form and how do i get the reference of this form in statemachine class,b'coz when i use radio buttons(which are place on welcome form), it give null pointer exception – gaurav Jul 18 '11 at 06:39
  • Already you got the `Form` from statemachine class using `startApp(...);`. Right? – bharath Jul 18 '11 at 06:47
  • this is my code:Form fo = (Form)cl.startApp(resources,"/NEW AADHAR.res",true); Form f= cl.findWelcome(fo); f.addCommandListener(this); bgVerification=cl.findVerification(f); bgEnrollment=cl.findEnrollment(f); bg.add(bgVerification); bg.add(bgEnrollment); where bg is button group and this is my button code: protected void onWelcome_ButtonAction(Component c, ActionEvent event) { super.onWelcome_ButtonAction(c, event); if(bgVerification.isSelected())//this line give error showForm("Login",null); else Dialog.show("INFORMATION","Please select option", "OK","Cancel"); – gaurav Jul 18 '11 at 07:09
  • thanks for your reply i used same thing u mentioned but this line in OKbutton code give error null pointer exception---if(bgVerification.isSelected()) – gaurav Jul 18 '11 at 09:20
  • i use statemachine constructor for declaring above radio buttons,i declare bg as buttongroup so this is fine ?bgVerification and bgEnrollment StateMachine() { try{ resources = Resources.open("/NEW AADHAR.res"); } catch(java.io.IOException err) { err.printStackTrace(); } cl.setHomeForm("Welcome"); Form fo = (Form)cl.startApp(resources,"/NEW AADHAR.res",true); //Form f= cl.findWelcome(fo); fo.addCommandListener(this); bgVerification=(RadioButton)cl.findVerification(fo); bgEnrollment=cl.findEnrollment(fo); } – gaurav Jul 18 '11 at 09:29
  • Why you are calling `super.onWelcome_ButtonAction(c, event);` and use `hasFocus()` instead of `isSelected()` – bharath Jul 18 '11 at 09:30
  • actually resourceEdit generate this super.onWelcome_ButtonAction(c,event) automatically when i create button "OK" – gaurav Jul 18 '11 at 09:35
  • Oh ok. Because I didn't use these functionality. What you get using `hasFocus()`? also use `bg.setSelected(0)`. this is set the default selected index to 0. – bharath Jul 18 '11 at 09:40
  • i get same error NullPointerException at-----if(Verification.hasFocus()) – gaurav Jul 18 '11 at 09:47
  • hi bharath,i tried your code but i get same error NullPointerException at-----if(Verification.hasFocus()) – gaurav Jul 20 '11 at 10:27
  • What you called in main midlet class? `StateMachine()` or `StateMachine(resources)` ? – bharath Jul 20 '11 at 10:29
  • StateMachine(resources),but currently i am running my desktop module,not MIDP – gaurav Jul 20 '11 at 10:36
  • i am using if(Verification.hasFocus()) and when i click nothing happens – gaurav Jul 20 '11 at 10:41
  • Ok. So you can remove the code from `StateMachine()` constructor and just initialize `Verification = (RadioButton) c;` before you checking `if(Verification.hasFocus())` – bharath Jul 20 '11 at 11:10
  • where i have to initialize Verification – gaurav Jul 20 '11 at 12:46
  • Look at the coding clearly. see the `onWelcome_ButtonAction(Component c, ...)` method. – bharath Jul 20 '11 at 12:54
  • this is my code::::: protected void onWelcome_ButtonAction(Component c, ActionEvent event) { // If the resource file changes the names of components this call will break notifying you that you should fix the code super.onWelcome_VerificationAction(c, event); if(Verification.hasFocus()) { showForm("Login",null); } else if(Enrollment.hasFocus()) { showForm("Authentication",null); } else { Dialog.show("INFORMATION","Please select option","OK","CANCEL"); } } – gaurav Jul 20 '11 at 12:58
  • java.lang.ClassCastException: com.sun.lwuit.Button cannot be cast to com.sun.lwuit.RadioButton i got this error at Verification=(RadioButton) c; – gaurav Jul 20 '11 at 13:30
  • Its working fine for me. What did you use in ResourceEdit#GUI? – bharath Jul 20 '11 at 13:33
  • I dont understand your problem here. Why you cant create own resource for your application? Are you using `RadioButton` on GUI? If you are adding RadioButton and you add the listener for this means `Component` should return `RadioButton` only. – bharath Jul 20 '11 at 13:40
  • please elaborate your answer i can't get it. – gaurav Jul 20 '11 at 18:39
  • Will you create `onWelcome_ButtonAction` listener for `RadioButton`? – bharath Jul 21 '11 at 05:07
  • i write Verification.addActionListener(this); – gaurav Jul 21 '11 at 17:36
  • `onWelcome_ButtonAction` are you using this action event for button or radiobutton? – bharath Jul 22 '11 at 05:33
  • hi bharath how can i do Verification.addActionCommand() b'coz lwuit don't have this method i think b'coz of this radio button is not working – gaurav Jul 23 '11 at 16:22
  • use `Verification.addActionListener(...);` – bharath Jul 26 '11 at 07:23
  • should i use Verification.addActionListener(this) in onWelcome_ButtonAction() method? – gaurav Jul 26 '11 at 09:23
  • No. Have use any `Button` on the same screen? – bharath Jul 26 '11 at 09:27
  • yes i use EXIT button and it works fine but i don't write any code for it i just add exit command for this button----- public static final int COMMAND_WelcomeEXIT = 4; protected boolean onWelcomeEXIT() { return false; } protected void processCommand(ActionEvent ev, Command cmd) { switch(cmd.getId()) { case COMMAND_WelcomeEXIT: if(onWelcomeEXIT()) { ev.consume(); } return; } } – gaurav Jul 26 '11 at 09:46
  • this code is generated by resource editor in statemachinebase class – gaurav Jul 26 '11 at 09:47
  • Then what is `onWelcome_ButtonAction`? where did add the `onWelcome_ButtonAction` event? which `Component` you are using for this event? – bharath Jul 26 '11 at 10:02
  • hi bharath i have edit my question and paste the code of statenachine class – gaurav Jul 26 '11 at 10:43
  • Clearly i understood you dont understand the flow. I asked lot of times, `onWelcome_ButtonAction` is one of the event(from your code). where you add this event? which component you are using? check it in created GUI at ResourceEdit file – bharath Jul 26 '11 at 11:10
  • i add this event on radio button – gaurav Jul 26 '11 at 12:18
  • See my update 3. Here i already described clearly. Dont use your posted code. use update 3 code. – bharath Jul 26 '11 at 12:25
  • it give error at Verification=(RadionButton)c; java.lang.ClassCastException: com.sun.lwuit.Button cannot be cast to com.sun.lwuit.RadioButton – gaurav Jul 26 '11 at 12:43
  • You said `onWelcome_ButtonAction` event for `RadioButton`. Then how its return as `Button`? check it in `ResourceEdit#GUI#RadioButton#ActionEvent`. – bharath Jul 26 '11 at 12:46
  • in statemachinebase class these methods return radiobutton::::::::: public com.sun.lwuit.RadioButton findVerification(Container root) { return (com.sun.lwuit.RadioButton)findByName("Verification", root); } public com.sun.lwuit.RadioButton findEnrollment(Container root) { return (com.sun.lwuit.RadioButton)findByName("Enrollment", root); } – gaurav Jul 26 '11 at 12:52
  • If you are adding `onWelcome_ButtonAction` event to `RadioButton` means it should return `Component c` as `RadioButton`. check it in `ResourceEdit` and choose your `GUI` and select the `RadioButton` and select the `Event` tab and then click the `Action Event` button. See what is happening and let me. – bharath Jul 26 '11 at 13:06
  • when i choose Action Event i got this method in statemachine class::::::::::::: protected void onWelcome_VerificationAction(Component c, ActionEvent event) { // If the resource file changes the names of components this call will break notifying you that you should fix the code super.onWelcome_VerificationAction(c, event); } – gaurav Jul 26 '11 at 13:43
  • Ok. So use this code `Verification = (RadioButton) c; // initialize here if (Verification.hasFocus()) { showForm("Login",null); } else { Dialog.show("INFORMATION", "Please select option", "OK", "Cancel"); } ` – bharath Jul 26 '11 at 13:49