Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of checkboxes.

 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ResultWindow extends JFrame{
int width=300,height=300;
JCheckBox[] ans;
ResultWindow(){
setSize(300,300);
setTitle("Result");
setVisible(true);
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++){
c.add(ans[i]);
}

setContentPane(c);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new ResultWindow();
}
}


I tried the code as above but no use
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
I got the loop-hole.
Now its working fine
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is ans[i]

read the code, think about it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic