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 17 of 17

Thread: Checking the Status of a checkbox

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

    Default Checking the Status of a checkbox

    Hello,
    I have essentially a preferences window with a bunch of boolean checkboxes. In another class, I need to check the status of these checkboxes (checked or unchecked), but I don't know how to do that since I can't make another instance of the class (as the boolean values wouldn't reflect those of the preferences runner class). Any help with this would be much appreciated! thanks!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Checking the Status of a checkbox

    By checkboxes, I assume you mean JCheckBox's. To access the status, pass a reference to the client class of the class which contains the JCheckBoxes. It then has access to them...if they are private variables, make public getters of their status.

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

    Default Re: Checking the Status of a checkbox

    Quote Originally Posted by copeg View Post
    By checkboxes, I assume you mean JCheckBox's. To access the status, pass a reference to the client class of the class which contains the JCheckBoxes. It then has access to them...if they are private variables, make public getters of their status.
    Well they're mCheckboxes (in android) and while I have public getters, I can only use them on an instance of a class. For example (the runner class is named Checkboxes)
    Checkboxes c = new Checkboxes();

    c.getStatus();


    but in order to use these getters , i'd have to make a new instance like i did above, which wouldn't reflect their state in the other window. How can I pass this status to other classes?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Checking the Status of a checkbox

     
    public class TheOtherClass {
         private final Checkboxes checkers;
        public TheOtherClass(CheckBoxes c){
            this.checkers = c;
        }
        public void doCheck(){
            checkers.getStatus();///
        }
    }
    Then, when you instantiate TheOtherClass, you pass the reference to its constructor. If there are a few degrees of seperation between your CheckBoxes and TheOtherClass, you'll need to redesign you program a bit to be able to pass the reference.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Checking the Status of a checkbox

    preferences window with a bunch of boolean checkboxes
    class prefWindow {
      // Define bunch of checkboxes
      mCheckbox cb1 = ...
     
      // Get the status of our cb1
      public boolean get_cb1() {
         return cb1.getStatus();
      } 
      ..

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

    Default Re: Checking the Status of a checkbox

    Quote Originally Posted by Norm View Post
    class prefWindow {
      // Define bunch of checkboxes
      mCheckbox cb1 = ...
     
      // Get the status of our cb1
      public boolean get_cb1() {
         return cb1.getStatus();
      } 
      ..
    In prefWindow I can get the status, but to access cb1 in another class it would be impossible without making an instance of prefWindow.





    public class Checkbox extends ListActivity {
        /** Called when the activity is first created. */
     
    	private CheckBoxifiedTextListAdapter cbla;
    	// Create CheckBox List Adapter, cbla
    	private String[] items = {"Restaurants","Pizza", "Coffee Houses", "Bakeries", "Grocers", "Caterers/Chefs","Other" };
    	// Array of string we want to display in our list
     
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.checkbox);
     
            cbla = new CheckBoxifiedTextListAdapter(this);
            for(int k=0; k<items.length; k++)
            {
            	cbla.addItem(new CheckBoxes(items[k], false));
            }  
            // Display it
            setListAdapter(cbla);
        }

    is essentially the runner class for this. I need these items that are added to cbla to be seen whether or not they are true or false. There are getter methods in the Checkboxes class, but how could these new instantiations of Checkboxes be accessed in a totally different class.
    Last edited by michaelz; July 20th, 2010 at 07:45 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Checking the Status of a checkbox

    If the checkboxes are in prefWindow, the only way to access them is by having a reference to prefWindow. You wouldn't need to create a new instance. You would need a way to get a reference. Passing references is pretty common. What problem is there in your program?
    Is prefWindow a singleton? IE there is only ever one instance at a time? Have a static getRef() method to return a reference to it.

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

    Default Re: Checking the Status of a checkbox

    Quote Originally Posted by Norm View Post
    If the checkboxes are in prefWindow, the only way to access them is by having a reference to prefWindow. You wouldn't need to create a new instance. You would need a way to get a reference. Passing references is pretty common. What problem is there in your program?
    Is prefWindow a singleton? IE there is only ever one instance at a time? Have a static getRef() method to return a reference to it.
    Yes, there is only one instance at a time-the instance running in the onCreate() method of the Checkbox class. As you notice in the Checkbox class (i posted it above), I have a cbla. If I make this static, and then make a static getter method, can I still access the current state of each Checkboxes object, or will the values stay static?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Checking the Status of a checkbox

    You don't need to make all the variables and methods static. If you use the Singleton pattern, you only need a static varible to hold a reference to the one instance of the class and a static method to return that reference. The rest of the class would not be static.

    will the values stay static
    The values will change with the user's selections as usual.

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

    Default Re: Checking the Status of a checkbox

    so i did

    private static CheckBoxifiedTextListAdapter cbla;

    and new method
    public static getCbla()
    {
    return cbla;
    }

    however, the values of cbla never seem to change when called by other classes. It sees that the name is "restaurant" and everything, but it doesn't see that it is checked.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Checking the Status of a checkbox

    How is does the code see if "it is checked"?
    How does it see the name?

    Where is cbla given a value? Does the code only give it a value one time?
    Last edited by Norm; July 23rd, 2010 at 06:33 PM.

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

    Default Re: Checking the Status of a checkbox

    Quote Originally Posted by Norm View Post
    How is does the code see if "it is checked"?
    How does it see the name?

    Where is cbla given a value? Does the code only give it a value one time?
    I will post the code for the classes.

    Here is Checkbox
    public class Checkbox extends ListActivity {
        /** Called when the activity is first created. */
     
    	private static CheckBoxifiedTextListAdapter cbla;
    	// Create CheckBox List Adapter, cbla
    	private String[] items = {"Restaurants","Pizza", "Coffee Houses", "Bakeries", "Grocers", "Caterers/Chefs","Other" };
    	// Array of string we want to display in our list
     
     
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.checkbox);
     
            cbla = new CheckBoxifiedTextListAdapter(this);
            for(int k=0; k<items.length; k++)
            {
            	cbla.addItem(new CheckBoxes(items[k], false));
            }  
            // Display it
            setListAdapter(cbla);
        }
     
        /* Remember the other methods of the CheckBoxifiedTextListAdapter class!!!!
         * cbla.selectAll() :: Check all items in list
         * cbla.deselectAll() :: Uncheck all items
         */
     
        public static CheckBoxifiedTextListAdapter getCbla()
        {
     
        	return cbla;
        }
    }

    Checkboxes
    public class CheckBoxes implements Comparable<CheckBoxes> {
     
        private String mText = "";
        private boolean mChecked;    
        public CheckBoxes(String text, boolean checked) {
       	 /* constructor */ 
             mText = text;
             mChecked = checked;
        }
        public void setChecked(boolean value)
        {
       	 this.mChecked = value;
        }
        public boolean getChecked(){
       	 return this.mChecked;
        }
     
        public String getText() {
             return mText;
        }
     
        public void setText(String text) {
             mText = text;
        }
     
        /** Make CheckBoxifiedText comparable by its name */
        //@Override
        public int compareTo(CheckBoxes other) {
             if(this.mText != null)
                  return this.mText.compareTo(other.getText());
             else
                  throw new IllegalArgumentException();
        }
    }

    CheckBoxifiedTextListAdapter

    public class CheckBoxifiedTextListAdapter extends BaseAdapter {
     
         /** Remember our context so we can use it when constructing views. */
         private Context mContext;
     
         private List<CheckBoxes> mItems = new ArrayList<CheckBoxes>();
     
         public CheckBoxifiedTextListAdapter(Context context) {
              mContext = context;
         }
     
         public void addItem(CheckBoxes it) { mItems.add(it); }
     
         public void setListItems(List<CheckBoxes> lit) { mItems = lit; }
     
         /** @return The number of items in the */
         public int getCount() { return mItems.size(); }
     
         public CheckBoxes getItem(int position) { return mItems.get(position); }
     
         public void setChecked(boolean value, int position){
             mItems.get(position).setChecked(value);
         }
         public void selectAll(){
             for(CheckBoxes cboxtxt: mItems)
                  cboxtxt.setChecked(true);
             /* Things have changed, do a redraw. */
             this.notifyDataSetInvalidated();
         }
         public void deselectAll()
         {
             for(CheckBoxes cboxtxt: mItems)
                 cboxtxt.setChecked(false);
            /* Things have changed, do a redraw. */
            this.notifyDataSetInvalidated();
         }
     
         public boolean areAllItemsSelectable() { return false; }
     
         /** Use the array index as a unique id. */
         public long getItemId(int position) {
              return position;
         }
     
         /** @param convertView The old view to overwrite, if one is passed
          * @returns a CheckBoxifiedTextView that holds wraps around an CheckBoxifiedText */
         public View getView(int position, View convertView, ViewGroup parent){
              CheckBoxifiedTextView btv;
              if (convertView == null) {
                   btv = new CheckBoxifiedTextView(mContext, mItems.get(position));
              } else { // Reuse/Overwrite the View passed
                   // We are assuming(!) that it is castable!
            	   CheckBoxes src = mItems.get(position);
                   btv = (CheckBoxifiedTextView) convertView;
                   btv.setCheckBoxState(src.getChecked()); 
                   btv = (CheckBoxifiedTextView) convertView;
                   btv.setText(mItems.get(position).getText());
              }
              return btv;
         }
    }

    CheckboxifiedTextView

    public class CheckBoxifiedTextListAdapter extends BaseAdapter {
     
         /** Remember our context so we can use it when constructing views. */
         private Context mContext;
     
         private List<CheckBoxes> mItems = new ArrayList<CheckBoxes>();
     
         public CheckBoxifiedTextListAdapter(Context context) {
              mContext = context;
         }
     
         public void addItem(CheckBoxes it) { mItems.add(it); }
     
         public void setListItems(List<CheckBoxes> lit) { mItems = lit; }
     
         /** @return The number of items in the */
         public int getCount() { return mItems.size(); }
     
         public CheckBoxes getItem(int position) { return mItems.get(position); }
     
         public void setChecked(boolean value, int position){
             mItems.get(position).setChecked(value);
         }
         public void selectAll(){
             for(CheckBoxes cboxtxt: mItems)
                  cboxtxt.setChecked(true);
             /* Things have changed, do a redraw. */
             this.notifyDataSetInvalidated();
         }
         public void deselectAll()
         {
             for(CheckBoxes cboxtxt: mItems)
                 cboxtxt.setChecked(false);
            /* Things have changed, do a redraw. */
            this.notifyDataSetInvalidated();
         }
     
         public boolean areAllItemsSelectable() { return false; }
     
         /** Use the array index as a unique id. */
         public long getItemId(int position) {
              return position;
         }
     
         /** @param convertView The old view to overwrite, if one is passed
          * @returns a CheckBoxifiedTextView that holds wraps around an CheckBoxifiedText */
         public View getView(int position, View convertView, ViewGroup parent){
              CheckBoxifiedTextView btv;
              if (convertView == null) {
                   btv = new CheckBoxifiedTextView(mContext, mItems.get(position));
              } else { // Reuse/Overwrite the View passed
                   // We are assuming(!) that it is castable!
            	   CheckBoxes src = mItems.get(position);
                   btv = (CheckBoxifiedTextView) convertView;
                   btv.setCheckBoxState(src.getChecked()); 
                   btv = (CheckBoxifiedTextView) convertView;
                   btv.setText(mItems.get(position).getText());
              }
              return btv;
         }
    }



    then it is referenced in another class like this

    if (Checkbox.getCbla().getItem(0).getText().equalsIgnoreCase("Restaurants"))
    		{
    			Log.v("TAG","it found the text atleast");
    			if (Checkbox.getCbla().getItem(0).getChecked())
    			{
    				Log.v("TAG","it also noticed the box was checked.");
    			}
     
    		}

    The first if statement returns true, but the getChecked() method never returns true.

    Thanks!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Checking the Status of a checkbox

    If getChecked() never returns true, then "this.mChecked" must always be false when getChecked() is called.

    Try debugging your code by adding println("mChecked=" + mChecked); at every place mChecked's values changes.
    Then you'll see what that setting for the variable is before you call the getChecked() method.

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

    Default Re: Checking the Status of a checkbox

    Quote Originally Posted by Norm View Post
    If getChecked() never returns true, then "this.mChecked" must always be false when getChecked() is called.

    Try debugging your code by adding println("mChecked=" + mChecked); at every place mChecked's values changes.
    Then you'll see what that setting for the variable is before you call the getChecked() method.
    It seems to get the value initially and it stays constant. Is there some way i can make a listener to catch checking events?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Checking the Status of a checkbox

    Is there some way i can make a listener to catch checking events
    Possibly. Read the API doc for the class to see if it can have listeners.

    It seems to get the value initially and it stays constant
    What value? Set or unset?

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

    Default Re: Checking the Status of a checkbox

    Quote Originally Posted by Norm View Post
    Possibly. Read the API doc for the class to see if it can have listeners.


    What value? Set or unset?
    unset (false). That is the value it is initially set to

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Checking the Status of a checkbox

    If you're still having a problem, can you make a small program that compiles and executes and demonstrates the problem.

Similar Threads

  1. HTTP Status 500 -
    By mqt in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2010, 01:09 AM
  2. how to delete record from checking checkbox value.
    By -_- in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: March 12th, 2010, 06:09 AM
  3. Need java code to know the browser status
    By newnewgen in forum Java Theory & Questions
    Replies: 2
    Last Post: January 8th, 2010, 10:39 PM
  4. how to delete record based on checkbox selected checkbox
    By -_- in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: December 15th, 2009, 08:26 PM
  5. setEnabled causing checkbox to deselect
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2009, 11:36 AM