Go Back   Java Programming Forums > Java Standard Edition Programming Help > Object Oriented Programming


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 20-07-2010, 07:38 PM
Junior Member
 

Join Date: Jul 2010
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
michaelz is on a distinguished road
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!



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 20-07-2010, 07:49 PM
copeg's Avatar
Moderator
 
6 Highscores

Join Date: Oct 2009
Posts: 685
Thanks: 8
Thanked 150 Times in 142 Posts
copeg will become famous soon enoughcopeg will become famous soon enough
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.
Reply With Quote
  #3 (permalink)  
Old 20-07-2010, 10:04 PM
Junior Member
 

Join Date: Jul 2010
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
michaelz is on a distinguished road
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?
Reply With Quote
  #4 (permalink)  
Old 20-07-2010, 10:26 PM
copeg's Avatar
Moderator
 
6 Highscores

Join Date: Oct 2009
Posts: 685
Thanks: 8
Thanked 150 Times in 142 Posts
copeg will become famous soon enoughcopeg will become famous soon enough
Default Re: Checking the Status of a checkbox

java Code:
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.
Reply With Quote
  #5 (permalink)  
Old 20-07-2010, 10:42 PM
Norm's Avatar
Senior Member
 

Join Date: May 2010
Location: SW Missouri
Posts: 788
Thanks: 0
Thanked 141 Times in 140 Posts
Norm will become famous soon enoughNorm will become famous soon enough

I'm feeling Fine
Default Re: Checking the Status of a checkbox

Quote:
preferences window with a bunch of boolean checkboxes
Java Code
class prefWindow {
  // Define bunch of checkboxes
  mCheckbox cb1 = ...

  // Get the status of our cb1
  public boolean get_cb1() {
     return cb1.getStatus();
  } 
  ..
Reply With Quote
  #6 (permalink)  
Old 21-07-2010, 12:37 AM
Junior Member
 

Join Date: Jul 2010
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
michaelz is on a distinguished road
Default Re: Checking the Status of a checkbox

Quote:
Originally Posted by Norm View Post
Java Code
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.





Java Code
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; 21-07-2010 at 12:45 AM.
Reply With Quote
  #7 (permalink)  
Old 21-07-2010, 12:41 AM
Norm's Avatar
Senior Member
 

Join Date: May 2010
Location: SW Missouri
Posts: 788
Thanks: 0
Thanked 141 Times in 140 Posts
Norm will become famous soon enoughNorm will become famous soon enough

I'm feeling Fine
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.
Reply With Quote
  #8 (permalink)  
Old 23-07-2010, 06:06 AM
Junior Member
 

Join Date: Jul 2010
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
michaelz is on a distinguished road
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?
Reply With Quote
  #9 (permalink)  
Old 23-07-2010, 11:23 AM
Norm's Avatar
Senior Member
 

Join Date: May 2010
Location: SW Missouri
Posts: 788
Thanks: 0
Thanked 141 Times in 140 Posts
Norm will become famous soon enoughNorm will become famous soon enough

I'm feeling Fine
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.

Quote:
will the values stay static
The values will change with the user's selections as usual.
Reply With Quote
  #10 (permalink)  
Old 23-07-2010, 09:29 PM
Junior Member
 

Join Date: Jul 2010
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
michaelz is on a distinguished road
Default Re: Checking the Status of a checkbox

so i did

Java Code
private static CheckBoxifiedTextListAdapter cbla;
and new method
Java Code
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP Status 500 - mqt What's Wrong With My Code? 3 06-04-2010 06:09 AM
how to delete record from checking checkbox value. -_- JavaServer Pages: JSP & JSTL 2 12-03-2010 11:09 AM
Need java code to know the browser status newnewgen Java Theory & Questions 2 09-01-2010 03:39 AM
how to delete record based on checkbox selected checkbox -_- JavaServer Pages: JSP & JSTL 0 16-12-2009 01:26 AM
setEnabled causing checkbox to deselect dewboy3d AWT / Java Swing 3 21-05-2009 04:36 PM


100 most searched terms
Search Cloud
actionlistener actionlistener in java addactionlistener addactionlister arraylist value into hash buffered read two integer value in java create an abstract class called shape with abstract methods+java double to integer double to integer in java double to integer java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread main java.lang.outofmemoryerror java heap space in eclipse format double java get mouse position java how to convert list to map in java how to format doubles in java how to make a calculator in jframe using jcreator http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html iphone java java actionlistener java cos java deallocate java double format java double to int java format double java forum java forums java get mouse position java heap size exception java jbutton java nextline() java program to find dimensions of a room java programming codes using astirisks java programming forum java programming forums java.lang.outofmemoryerror: java heap space java.lang.reflect.invocationtargetexception jbutton actionlistener jbutton with key enter jtable questions in java jtext bold jtextarea font jxl.read.biff.biffexception: unable to recognize ole stream kilowatts java program mean value decimal double java oops java assignments programmer forum smack api messagelistener two dimensional arraylist in java

All times are GMT. The time now is 09:07 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.