permaculture playing cards*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Array of checkboxes." Watch "Array of checkboxes." New topic
Author

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
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

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;
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

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
 
 
subject: Array of checkboxes.
 
Similar Threads
Problem with JDialog and setSize()
Checkboxes?
A window to the centre of the screen
getting a JScrollPane to display its scrollbars
First time with applets