my dog learned polymorphism
The moose likes Swing / AWT / SWT and the fly likes Array of checkboxes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Array of checkboxes." Watch "Array of checkboxes." New topic

Array of checkboxes.

Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

I am trying to have array of check boxes on a frame...but checkboxes are not displaying...
Please guide me i am a newbie.. don't know very much.

[Added code tags - see UseCodeTags for details]
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

You haven't actually created any checkboxes anywhere.

At the moment, each time round the loop you're creating a new JCheckBox array, but all the elements in it are null. What you need to do is create the array once (probably assigning it to the class variable that you've comments out on line 3 - though please give it a more meaningful name!), and then create 5 JCheckBox objects to put in it.
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

class ResultWindow extends JFrame{
int width=300,height=300;
JCheckBox[] ans;
Toolkit toolkit = Toolkit.getDefaultToolkit ();
Dimension dim = toolkit.getScreenSize();
setLocation((dim.width-width)/2, (dim.height-height)/2);
Container c=getContentPane();
c.setLayout(new GridLayout(7,3));
ans=new JCheckBox[5];
for(int i=0;i<5;i++){

public static void main(String[] args){
new ResultWindow();

I tried the code as above but no use
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

Thank you.
I got the loop-hole.
Now its working fine
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
what is ans[i]

read the code, think about it
I agree. Here's the link:
subject: Array of checkboxes.
It's not a secret anymore!