Results 1 to 3 of 3
- 02-11-2012, 12:01 AM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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);When I try to run this I get: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()); } } }
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 ?
- 02-11-2012, 12:18 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Initialization Problem of CheckBoxes
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.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) } } }
You can check this with:
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.Java Code:MyCheckBox() { System.out.println("Creating a MyCheckBox instance"); myCheckBoxArrayList = new ArrayList<JCheckBox>(); // etc
- 02-11-2012, 12:19 AM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
variable initialization problem
By mitra in forum New To JavaReplies: 4Last Post: 07-26-2011, 07:22 AM -
Stack class/problem in stackSize Initialization and usage of pop() function
By Mazharul in forum New To JavaReplies: 1Last Post: 11-17-2008, 09:32 AM -
initialization value problem
By ravian in forum New To JavaReplies: 2Last Post: 01-28-2008, 10:54 AM -
Icon initialization problem
By saz25 in forum AWT / SwingReplies: 1Last Post: 12-24-2007, 10:37 PM -
Problem with checkboxes
By carl in forum Java AppletsReplies: 1Last Post: 08-06-2007, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks