Bookmark Topic Watch Topic
  • New Topic

How to add JPanels to a JPanel array after initializing them?  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am creating a chess program in netbeans GUI creator. I have visually created 64 JPanels, which are initialized in generated code in the following manner:

jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
etc.

Now I am trying to add the already initialized JPanels to an array, but cannot get it to work without a bunch of errors. I have tried an array initializer and also manually entering each section of the array in the following manners:

1) initializer
JPanel[][] chessBoard = {{jPanel1,jPanel2,jPanel3,jPanel4,jPanel5,jPanel6,jPanel7,jPanel8}{jPanel9,jPanel10,jPanel11, etc.}{etc}{etc}{etc}{etc}}

2) piece by piece
JPanel[][] chessBoard = new JPanel[8][8];
chessBoard[0][0] = jPanel1;
chessBoard[0][1] = jPanel2;
etc

However, both attempts cause the following error over and over again: "cannot find symbol: variable jPanel(whatever number)". What am I doing wrong? Is there a way to do what I am trying to do? Help would be much appreciated.
 
Bund De
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
You could try making a for loop and populate the array with "new JPanel()" declarations, and then refer to the panels with the array's index. Array[0], Array[1], etc...
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Report post to moderator
The problem is that I can't modify the declaration of the panels in the generated code section. I need to somehow add them to an array after creating them. I suspect that this is somehow possible because when looking up how to solve this problem on the netbeans website they gave me the following response:

"How Do I create an Array of GUI Controls in the NetBeans GUI Builder?
There is no IDE action dedicated to that. If the number of controls (the length of the array) is static, and you need the array only to loop over the controls, try this:
Add all components manually (via drag and drop). Then create an array in code and fill it with all the component variables."

http://wiki.netbeans.org/FaqGuiControlArray

Unclear on the details, but I think its saying to visually add all 64 jPanels, then somehow add them all to the same array. That's what I can't figure out how to do.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
I'ts important to remember that an array holds references, not objects. All you need to do is to assign the value of the concerned variable to a slot in the array.

I will add that the visual designer is categorically not a beginners' tool, and use of it by someone not yet adequately familiar with Java and Swing GUIs inevitable leads to problems like this one. You would be far better off learning to use layouts and coding your GUI by hand. It may take a little longer to do the initial layout, but you save the time you waste in figuring out the quirks of the designer.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please UseOneThreadPerQuestion <-link

I'm locking this as a duplicate of http://www.coderanch.com/t/613706/java/java/Adding-Initialized-JPanels-array-causing
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!