Results 1 to 3 of 3
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Initialization Problem of CheckBoxes

    Java Code:
    public class MainFrame extends JFrame{
    
    	MainFrame()
    	{
    		//I have some code until here that is ok..
    		JPanel upperLeftPanel = new JPanel(new GridLayout(4,3));
    		centerPanel.add(upperLeftPanel);
    		
    		MyCheckBox testing = new MyCheckBox();
    		
    		for(int i=0;i<6;i++)
    		{
    			upperLeftPanel.add(testing.myCheckBoxArrayList.get(i));
    		}
    		
    		this.setVisible(true);
    Java Code:
    public class MyCheckBox extends JCheckBox
    {
    	ArrayList<JCheckBox> myCheckBoxArrayList;
    	
    	MyCheckBox()
    	{
    		myCheckBoxArrayList = new ArrayList<JCheckBox>();
    		
    		for(int i=0;i<6;i++)
    		{
    			myCheckBoxArrayList.add(i, new MyCheckBox());
    		}
    	}
    }
    When I try to run this I get:

    Exception in thread "main" java.lang.StackOverflowError
    at java.util.Hashtable.get(Unknown Source)
    at javax.swing.UIDefaults.getFromHashtable(Unknown Source)
    at javax.swing.UIDefaults.get(Unknown Source)
    at javax.swing.MultiUIDefaults.get(Unknown Source)
    ... and many more..

    Any help ?
    What am I doing wrong here ?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Initialization Problem of CheckBoxes

    Java Code:
    public class MyCheckBox extends JCheckBox
    {
        ArrayList<JCheckBox> myCheckBoxArrayList;  // <-- (1)
         
        MyCheckBox()
        {
            myCheckBoxArrayList = new ArrayList<JCheckBox>();
             
            for(int i=0;i<6;i++)
            {
                myCheckBoxArrayList.add(i, new MyCheckBox()); // <-- (2)
            }
        }
    }
    Every instance of MyCheckBox contains a collection of checkboxes (1). And you put six new MyCheckBox instances into that collection (2). But each new MyCheckBox instance you put into that collection will have its own collection and so on. You get into an infinite loop creating more and more MyCheckBox instances.

    You can check this with:

    Java Code:
    MyCheckBox()
    {
        System.out.println("Creating a MyCheckBox instance");
        myCheckBoxArrayList = new ArrayList<JCheckBox>();
        // etc
    I don't know what myCheckBoxArrayList is supposed to represent. But ask yourself whether whatever it represents is the sort of thing that is a property of a check box. If it is not, then it belongs somewhere else.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Initialization Problem of CheckBoxes

    Thanks, I was just going to answer my own question.

    Thanks for your time..

    I am trying to create a Yatchzee game, just the Swing part is crazy difficult ( for me )...

Similar Threads

  1. variable initialization problem
    By mitra in forum New To Java
    Replies: 4
    Last Post: 07-26-2011, 07:22 AM
  2. Replies: 1
    Last Post: 11-17-2008, 09:32 AM
  3. initialization value problem
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 10:54 AM
  4. Icon initialization problem
    By saz25 in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 10:37 PM
  5. Problem with checkboxes
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •