Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: CheckboxGroup problem

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default CheckboxGroup problem

    import java.awt.event.*;
     import java.awt.*;
     import javax.swing.*;
     
     
    public class testNi extends Frame implements ActionListener {
        public Label first,second,result;
        public Button submit,clear;
        public TextField num1, num2;
        public Panel p,p1,p2,p3;
        CheckboxGroup c = new CheckboxGroup();
        Checkbox r1,r2,r3,r4; 
     
        public testNi(String title) {
            super(title);
            setLayout(new FlowLayout());
            p = new Panel();
            p1 = new Panel();
            p2 = new Panel();
            p3 = new Panel();
            first = new Label("Enter 1st number");
            second = new Label("Enter 2nd number");
            num1 = new TextField(20);
            num2 = new TextField(20);
            submit = new Button("Submit");
            clear = new Button("Clear");
            result = new Label("                        ");
     
            p3.add(r1=new Checkbox("addition",c,true));
            p3.add( new Checkbox("subtraction",c,false));
            p3.add( new Checkbox("multiplication",c,false));
            p3.add( new Checkbox("division",c,false));
            p.add(first);
            p.add(num1);
            p1.add(second);
            p1.add(num2);
            p2.add(submit);
            p2.add(clear);
     
     
            num1.addActionListener(this);
            num2.addActionListener(this);
            submit.addActionListener(this);
            clear.addActionListener(this);
     
     
            add(p3);
            add(p);
            add(p1);
            add(p2);
            add(result);
     
     
        }
     
        public void actionPerformed(ActionEvent e){
          double ans=0.0;
     
        if(e.getSource() == submit){
             if(r1.getState() == true){
                 ans = Double.parseDouble(num1.getText()) + Double.parseDouble(num2.getText());
                 result.setText("Answer: " + ans);
             }
         }else if(e.getSource() == submit){
             if(r2.getState()==true){
                 ans = Double.parseDouble(num1.getText()) - Double.parseDouble(num2.getText());
                 result.setText("Answer: " + ans);
             }
         }else if(e.getSource() == submit){
             if(r3.getState()==true){
                 ans = Double.parseDouble(num1.getText()) * Double.parseDouble(num2.getText());
                 result.setText("Answer: " + ans); 
             }
         }
         else if(e.getSource() == submit){
             if(r4.getState()==true){
                 ans = Double.parseDouble(num1.getText()) / Double.parseDouble(num2.getText());
                 result.setText("Answer: " + ans);
             }
     
         }else if(e.getSource() == clear){
              num1.setText("");
              num2.setText("");
              result.setText("");
         }
     }
     
     
        public static void main(String []args){
            testNi f = new testNi("This is just a simple test!");
            f.setSize(400,200);
            f.setVisible(true);
        }
     
    }

    the problem here in this code is under in actionPerformed, I have 4 radio buttons. whatever button is selected that will be the operation to be used in my textFields. but the problem is only the addition is working. when im going to change the getState() to getSelectedCheckbox() an error will appear and it says cannot find symbol getSelectedCheckbox. what's wrong with my code?please do help..
    thanks a lot in advance..


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: CheckboxGroup problem

    What's a CheckboxGroup?

    db

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: CheckboxGroup problem

    an error will appear and it says cannot find symbol getSelectedCheckbox
    Please copy and paste the full text of the error message here.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: CheckboxGroup problem

    Quote Originally Posted by joy_blue03 View Post

    the problem here in this code is under in actionPerformed, I have 4 radio buttons. whatever button is selected that will be the operation to be used in my textFields. but the problem is only the addition is working. when im going to change the getState() to getSelectedCheckbox() an error will appear and it says cannot find symbol getSelectedCheckbox. what's wrong with my code?please do help..
    thanks a lot in advance..
    It sounds dumb, but make sure you included the brackets after getSelectedCheckbox. What I mean is, make sure you typed: getSelectedCheckbox(); instead of getSelectedCheckbox;

    I've made that mistake SO many times, and it usually results in the compiler giving you that error because it is looking for you attempting to reference a variable in the checkbox (or whatever is being referenced) class.

    Thats the first thing I would do.

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CheckboxGroup problem

    @ daryl: its a radio button
    @ norm here's the error when i change to getSelectedCheckbox() :

    C:\Users\Faculty\cs257\src\testNi.java:60: cannot find symbol
    symbol : method getSelectedCheckboxGroup()
    location: class java.awt.Checkbox
    if(r1.getSelectedCheckboxGroup() == true){

    @ aussiemcgr: i think i posted it correctly, try to review..
    < when im going to change the getState() to getSelectedCheckbox() >

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: CheckboxGroup problem

    How come the code you've posted doesn't have the code in it the you show in the posted error message?

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: CheckboxGroup problem

    Quote Originally Posted by joy_blue03 View Post
    @ daryl: its a radio button
    @ norm here's the error when i change to getSelectedCheckbox() :

    C:\Users\Faculty\cs257\src\testNi.java:60: cannot find symbol
    symbol : method getSelectedCheckboxGroup()
    location: class java.awt.Checkbox
    if(r1.getSelectedCheckboxGroup() == true){

    @ aussiemcgr: i think i posted it correctly, try to review..
    < when im going to change the getState() to getSelectedCheckbox() >
    Ok, how about this. You have the APIs mixed up.

    CheckboxGroup.getSelectedCheckbox() returns a Checkbox
    Checkbox.getState() returns a boolean.

    What you are doing is Checkbox.getSelectedCheckbox() which doesnt exist.
    maybe what you want is:

    public void actionPerformed(ActionEvent e){
          double ans=0.0;
     
        if(e.getSource() == submit){
           Checkbox temp = c.getSelectedCheckbox();
           if(temp==r1)
               ans = Double.parseDouble(num1.getText()) + Double.parseDouble(num2.getText());
           else if(temp==r2)
              ans = Double.parseDouble(num1.getText()) - Double.parseDouble(num2.getText());
           else if(temp==r3)
              ans = Double.parseDouble(num1.getText()) * Double.parseDouble(num2.getText());
           else if(temp==r4)
              ans = Double.parseDouble(num1.getText()) / Double.parseDouble(num2.getText());
     
           result.setText("Answer: " + ans);
     
         }else if(e.getSource() == clear){
              num1.setText("");
              num2.setText("");
              result.setText("");
         }
    }

    I noticed you typed: else if(e.getSource() == submit) several times. Keep in mind that if the first if is incorrect, all of these will be skipped as they will ALL have the value false. For what you are trying to do, you only need 2 conditions.

    Also, r2, r3, and r4 were never initialized. That would be why only r1 was working. However it's true you created 3 other Checkboxes, you never assigned them so they just got garbage collected.
    Last edited by aussiemcgr; July 8th, 2010 at 05:32 PM.

  8. #8
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CheckboxGroup problem

    thanks for the help. I already fix the problem.