Results 1 to 6 of 6
  1. #1
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Unable to access state of JCheckbox in separate class

    I am trying to make a program that will eventually display the total for a pizza based on user selection

    The problem I am running into is that whenever I run my program, it will add 50 cents to the total if I check OR uncheck the JCheckBox. I
    What I attempted to do to remedy this was to add another condition on line 66 that uses the isSelected() method to see if the box was being checked or unchecked, however now it is not working. I tried to set bacon as static to make it visible, however this breaks it as well.

    I would really appreciate any help.


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Pizza extends JPanel implements ActionListener 
    {	static int large = 5;
    	static int medium = 4;
    	static int small = 3;
    	public JTextArea textArea = new JTextArea();
    	public float total = 0;
        public Pizza()
       		{	//String output = "cost";
        		setLayout (new BorderLayout());
                setBackground (Color.white);
                
                JPanel b1 = new JPanel(new GridLayout(0,1));
                JButton b2 = new JButton ("Top");
                JPanel b3 = new JPanel(new GridLayout(0,1));
                
                JRadioButton large = new JRadioButton("Large");
                JRadioButton medium = new JRadioButton("Medium");
                JRadioButton small = new JRadioButton("Small");
                JCheckBox bacon = new JCheckBox("bacon");
                ButtonGroup size = new ButtonGroup();
                size.add(large);
                size.add(medium);
                size.add(small);
                
                b3.add(textArea);
                
                
                large.addActionListener(this);
                medium.addActionListener(this);
                small.addActionListener(this);
                bacon.addActionListener(this);
               
                b1.add(large);
                b1.add(medium);
                b1.add(small);
                b1.add(bacon);
               
    
                add (b1, BorderLayout.CENTER);
                add (b2, BorderLayout.NORTH);
                add (b3, BorderLayout.EAST);
                 
       		}
       public void actionPerformed(ActionEvent e) 
       		{	String input=e.getActionCommand();
       			
       			if (input == "Small")
       				{	total=0;
       					total+=5;
       				}
       			
       			else if (input == "Medium")
    				{	total=0;
    					total+=7;
    				}
    			
       			else if (input == "Large")
    				{	total=0;
    					total+=10;
    				}
    			
       			else if (input == "bacon" && bacon.isSelected()=1)
    				{	total+=0.50;
    				}
       			
    			
       			
       			String totalString = total +  "";
       			textArea.setText(totalString);
       		}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Unable to access state of JCheckbox in separate class

    Java Code:
    else if (input == "bacon" && bacon.isSelected()=1)
    Does that compile? If you get compiler messages you cannot understand it is a good idea to post the messages and say which lines of your code they are referring to.

    In Java 1 has nothing to do with true and = is used for assignment not comparison. If you want to check the bacon state you probably mean something like bacon.isSelected() for "having bacon" and !bacon.isSelected() for "not having bacon".

  3. #3
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Unable to access state of JCheckbox in separate class

    Allright so I changed the line to
    Java Code:
    else if (input == "bacon" && bacon.isSelected())
    but as soon as I check the box it breaks apart. Also eclipse is telling me that "bacon cannot be resolved"

    the errors that I get when I check bacon is


    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
    bacon cannot be resolved

    at Pizza.actionPerformed(Pizza.java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.JToggleButton$ToggleButtonModel.setPre ssed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Unable to access state of JCheckbox in separate class

    "breaks apart" is not very meaningful.

    -----

    "bacon cannot be resolved" is a compiler message. Since the program does not compile it's a bit pointless to run it. (Indeed it's not quite clear *what* Eclipse will be running...) The first order of business is to address the compiler message.

    The compiler message applies to this line:

    Java Code:
    else if (input == "bacon" && bacon.isSelected())
    and it means, in effect, that the compiler has no clue what bacon refers to.

    Variables in Java have scope. The scope of a variable (or name generally) is that area of your code where the variable can be validly used. The rule is mostly very simple: once you declare a variable it is in scope until the end of the block where it is declared. The "block" extends up to the } which matches the { just before where you declare the variable.

    Java Code:
    public Pizza()
        {   //String output = "cost"; // <--------  This { starts the block
            setLayout (new BorderLayout());
            setBackground (Color.white);
              
            JPanel b1 = new JPanel(new GridLayout(0,1));
            JButton b2 = new JButton ("Top");
            JPanel b3 = new JPanel(new GridLayout(0,1));
                 
            JRadioButton large = new JRadioButton("Large");
            JRadioButton medium = new JRadioButton("Medium");
            JRadioButton small = new JRadioButton("Small");
            JCheckBox bacon = new JCheckBox("bacon"); // <---------- here bacon is declared
            ButtonGroup size = new ButtonGroup();
            size.add(large);
            size.add(medium);
            size.add(small);
                 
            b3.add(textArea);
                 
                 
            large.addActionListener(this);
            medium.addActionListener(this);
            small.addActionListener(this);
            bacon.addActionListener(this);
                
            b1.add(large);
            b1.add(medium);
            b1.add(small);
            b1.add(bacon);
                
     
            add (b1, BorderLayout.CENTER);
            add (b2, BorderLayout.NORTH);
            add (b3, BorderLayout.EAST);
                  
        } // <------- this } ends the block.  bacon cannot be validly used from now on
    bacon is said to be "out of scope" when you want to use it later. And the solution is to give it a larger scope: declare bacon as an instance variable (as you do with textArea) then you will be able to use it throughout the class (as you do with textArea).

  5. #5
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Unable to access state of JCheckbox in separate class

    Ah, thank you so much, that fixed it!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Unable to access state of JCheckbox in separate class

    You're welcome.

    A small thing that might clean up the code a bit:

    Java Code:
    if (input == "Small")
       {   total=0;
            total+=5;
       }
    would be a little clearer as

    Java Code:
    if (input == "Small")
       {   
            total = 5;
       }

Similar Threads

  1. Unable to access getter method in an object class I made
    By EnSlavingBlair in forum New To Java
    Replies: 6
    Last Post: 10-01-2011, 11:52 AM
  2. Modifying JLabel within separate class
    By the_rider9 in forum New To Java
    Replies: 6
    Last Post: 07-06-2011, 07:07 PM
  3. how to separate this code in another class
    By Jhovarie in forum AWT / Swing
    Replies: 5
    Last Post: 03-01-2011, 06:08 PM
  4. Access Remote Session Bean from an separate web app
    By kiyoharatakuya in forum Enterprise JavaBeans (EJB)
    Replies: 4
    Last Post: 04-24-2009, 04:46 PM
  5. JSP Unable to access the third party libraries
    By VJSFDeveloper in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 09-08-2008, 05:57 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
  •